# rs/zerolog architecture

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

- Interactive diagram: https://gitdiagram.com/rs/zerolog
- Repository: https://github.com/rs/zerolog

## Overview

##### Main Components

a. Core Components:
- Logger Core (zerolog.Logger)
- Event System
- Encoders (JSON/CBOR)
- Context Management
- Level Management
- Field Management

b. Integration Components:
- HTTP Integration (hlog package)
- Context Integration
- Standard Logger Bridge

c. Output Components:
- Console Writer
- Multi-Level Writer
- Diode Writer (Non-blocking)

##### Key Relationships

a. Data Flow:
- Log Event Creation → Field Addition → Encoding → Output
- Context Propagation
- Level Filtering
- Sampling Logic

b. Integration Points:
- HTTP Middleware Chain
- Context Integration
- Hook System

**[Application Layer]**

- ▼
**[Logger Interface]**

- [Context Management]
- Context Fields

- [Event Processing]
- Level Filter
- Sampling
- Hook System

- [Field Management]
- Standard Fields
- Custom Fields

- [Encoding Layer]
- JSON Encoder
- CBOR Encoder

- [Output System]
- Console Writer
- Multi-Level Writer
- Diode Writer

##### Implementation Details

- Clear labels for each component
- Notes for key performance characteristics
- Configuration points

##### Additional Elements

- Performance metrics where relevant
- Integration examples with common frameworks
- Extension points

##### Key Architectural Patterns to Highlight

- Chain-based API design
- Context propagation
- Hook system
- Sampling mechanism
- Multi-output support

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "Application Layer"
        App[Application]
    end

    subgraph "Logger Interface"
        Logger["Logger Core"]:::core
        click Logger "https://github.com/rs/zerolog/blob/master/log.go"
    end

    subgraph "Context Management"
        CtxMgmt["Context Handler"]:::core
        CtxFields["Context Fields"]:::core
        click CtxMgmt "https://github.com/rs/zerolog/blob/master/context.go"
        click CtxFields "https://github.com/rs/zerolog/blob/master/ctx.go"
    end

    subgraph "Event Processing"
        EventSys["Event System"]:::processing
        Sampling["Sampling System"]:::processing
        Hooks["Hook System"]:::processing
        click EventSys "https://github.com/rs/zerolog/blob/master/event.go"
        click Sampling "https://github.com/rs/zerolog/blob/master/sampler.go"
        click Hooks "https://github.com/rs/zerolog/blob/master/hook.go"
    end

    subgraph "Field Management"
        Fields["Field Handler"]:::core
        Arrays["Array Handler"]:::core
        ErrorStacks["Error Stack Traces"]:::core
        click Fields "https://github.com/rs/zerolog/blob/master/fields.go"
        click Arrays "https://github.com/rs/zerolog/blob/master/array.go"
        click ErrorStacks "https://github.com/rs/zerolog/blob/master/pkgerrors/stacktrace.go"
    end

    subgraph "Encoding Layer"
        JsonEnc["JSON Encoder"]:::encoding
        CborEnc["CBOR Encoder"]:::encoding
        click JsonEnc "https://github.com/rs/zerolog/blob/master/encoder_json.go"
        click CborEnc "https://github.com/rs/zerolog/blob/master/encoder_cbor.go"
    end

    subgraph "Output System"
        Writers["Writer System"]:::output
        Console["Console Writer"]:::output
        DiodeW["Diode Writer"]:::output
        Syslog["Syslog"]:::output
        Journal["Journal"]:::output
        click Writers "https://github.com/rs/zerolog/blob/master/writer.go"
        click Console "https://github.com/rs/zerolog/blob/master/console.go"
        click DiodeW "https://github.com/rs/zerolog/blob/master/diode/diode.go"
        click Syslog "https://github.com/rs/zerolog/blob/master/syslog.go"
        click Journal "https://github.com/rs/zerolog/blob/master/journald/journald.go"
    end

    subgraph "Integration Layer"
        HTTP["HTTP Integration"]:::integration
        StdLog["Standard Logger Bridge"]:::integration
        click HTTP "https://github.com/rs/zerolog/blob/master/hlog/hlog.go"
        click StdLog "https://github.com/rs/zerolog/blob/master/log/log.go"
    end

    %% Relationships
    App --> Logger
    Logger --> CtxMgmt
    Logger --> EventSys
    Logger --> Fields
    
    CtxMgmt --> CtxFields
    
    EventSys --> Sampling
    EventSys --> Hooks
    
    Fields --> Arrays
    Fields --> ErrorStacks
    
    EventSys --> JsonEnc
    EventSys --> CborEnc
    
    JsonEnc --> Writers
    CborEnc --> Writers
    
    Writers --> Console
    Writers --> DiodeW
    Writers --> Syslog
    Writers --> Journal
    
    Logger --> HTTP
    Logger --> StdLog

    %% Styles
    classDef core fill:#2196F3,stroke:#1565C0,color:white
    classDef processing fill:#FFC107,stroke:#FFA000,color:black
    classDef encoding fill:#4CAF50,stroke:#388E3C,color:white
    classDef output fill:#FF5722,stroke:#E64A19,color:white
    classDef integration fill:#9C27B0,stroke:#7B1FA2,color:white

    %% Legend
    subgraph Legend
        CoreL["Core Components"]:::core
        ProcessingL["Processing Components"]:::processing
        EncodingL["Encoding Components"]:::encoding
        OutputL["Output Components"]:::output
        IntegrationL["Integration Components"]:::integration
    end
```
