# vuduongtp/go-logadapter architecture

> Architecture diagram of the GitHub repository vuduongtp/go-logadapter, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/vuduongtp/go-logadapter
- Repository: https://github.com/vuduongtp/go-logadapter

## Overview

##### Project type and purpose
- This is an open-source Go library (“go-logadapter”) that sits between an application and Go’s logging packages to provide:
- Multi-destination logging (console, file)
- Customizable formats (JSON, pretty JSON, text)
- Log filtering by level or custom fields
- Pluggable adapters for Echo (HTTP framework) and Gorm (ORM)

##### File structure
- Top-level Go source files:
- constant.go: global constants (e.g., default log keys or formats)
- util.go: helper functions (e.g., context extraction, ID generators)
- echo_adapter.go: implements Echo’s Logger interface + middleware
- gorm_adapter.go: implements Gorm’s logger.Interface
- main.go: example/demo of setting up and running an Echo server
- test/test.go: end-to-end usage example
- go.mod / go.sum: dependency management
- README.md: project overview and usage instructions

##### README for insights
- Core concepts: Config struct, SetLogger/NewWithConfig, SetFormatter
- Two official adapters: EchoLogger & EchoLoggerMiddleware, GormLogger
- FileConfig for rotating file output

##### Main components & their interactions
A. Core Logging Engine (logadapter package)
– Exposes functions: Debug, InfoWithContext, Error, Warn, SetLogger, SetFormatter
– Holds a singleton “Logger” instance configured with:
- LogLevel, LogFormat, TimestampFormat
- Sinks: console writer and optional rotating file writer (FileConfig)
– Reads custom fields from context

B. Echo Adapter
– Implements echo.Logger interface
– Middleware injects request_id & correlation_id into request context
– Routes all Echo logs and HTTP request logs into the core logger

C. Gorm Adapter
– Implements gorm/logger.Interface
– On each SQL call, captures query, row count, latency
– Routes Gorm logs into the core logger with custom fields (type=”sql”)

D. File Writer (optional sink)
– Uses lumberjack or similar for log rotation: size, backups, age, compression

E. Client application (e.g., main.go or any user code)
– Calls logadapter.SetLogger(NewWithConfig(&Config{…}))
– Imports and uses logadapter.Debug, InfoWithContext, etc.
– For Echo: replaces e.Logger, adds middleware
– For Gorm: sets db.Logger = NewGormLogger()

##### Key architectural patterns & principles
- Dependency Injection via interfaces (Echo’s Logger, Gorm’s logger.Interface)
- Singleton / Global logger instance but configured at startup
- Strategy pattern for formatters (JSONFormat, PrettyJSONFormat, TextFormat)
- Decorator over standard loggers for multi-sink capability
- Context propagation for custom fields

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Core Logging Engine
    subgraph "Core Logging Engine"
        constant["constant.go"]:::core
        util["util.go"]:::core
        coreLogger["Logger Singleton\n(Debug, Info, Error,\nSetLogger, SetFormatter)"]:::core
    end

    %% Adapters
    subgraph "Adapters"
        EchoAdapter["Echo Adapter\n(echo_adapter.go)"]:::adapters
        GormAdapter["Gorm Adapter\n(gorm_adapter.go)"]:::adapters
    end

    %% External Frameworks
    subgraph "External Frameworks"
        Echo["Echo Framework"]:::external
        Gorm["Gorm ORM"]:::external
    end

    %% Application Code
    subgraph "Application Code"
        Main["main.go\n(Demo Setup)"]:::application
        Test["test/test.go\n(End-to-End Example)"]:::application
    end

    %% Output Sinks
    subgraph "Sinks"
        Console["Console Sink"]:::sinks
        File["File Sink\n(Rotating Writer)"]:::sinks
    end

    %% Connections
    Main -->|"SetLogger(NewWithConfig)"| coreLogger
    Test -->|"SetLogger/NewWithConfig"| coreLogger

    Echo -->|"echo logs\nHTTP request logs"| EchoAdapter
    EchoAdapter -->|"LogMessage(level,msg,ctx)"| coreLogger

    Gorm -->|"SQL logs"| GormAdapter
    GormAdapter -->|"LogMessage(level,msg,ctx,type=sql)"| coreLogger

    coreLogger -->|"LogMessage"| Console
    coreLogger -->|"LogMessage"| File

    %% Click Events
    click constant "https://github.com/vuduongtp/go-logadapter/blob/main/constant.go"
    click util "https://github.com/vuduongtp/go-logadapter/blob/main/util.go"
    click EchoAdapter "https://github.com/vuduongtp/go-logadapter/blob/main/echo_adapter.go"
    click GormAdapter "https://github.com/vuduongtp/go-logadapter/blob/main/gorm_adapter.go"
    click Main "https://github.com/vuduongtp/go-logadapter/blob/main/main.go"
    click Test "https://github.com/vuduongtp/go-logadapter/blob/main/test/test.go"

    %% Styles
    classDef core fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef adapters fill:#98FB98,stroke:#000,stroke-width:1px
    classDef external fill:#FFA500,stroke:#000,stroke-width:1px
    classDef sinks fill:#DDA0DD,stroke:#000,stroke-width:1px
    classDef application fill:#D3D3D3,stroke:#000,stroke-width:1px
```
