logging

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 27, 2026 License: MIT Imports: 6 Imported by: 0

README

Logging Package

Structured logging utilities for the LLM proxy, built on Zap.

Purpose & Responsibilities

  • Structured Logging: JSON and console output formats via Zap
  • Canonical Fields: Standardized field names for consistent log analysis
  • Context Propagation: Request ID and correlation ID tracking
  • Log Level Filtering: Configurable verbosity (debug, info, warn, error)
  • Output Routing: Stdout or file-based log output

Configuration Options

Environment Variable Description Default
LOG_LEVEL Minimum log level (debug, info, warn, error) info
LOG_FORMAT Output format (json, console) json
LOG_FILE File path for log output (empty = stdout) -

Log Levels

Level Description Use Case
debug Verbose debugging information Development, troubleshooting
info Normal operational messages Production default
warn Potentially harmful situations Configuration issues, deprecations
error Error conditions Failures requiring attention

Canonical Log Fields

The package provides helper functions for consistent field naming:

Helper Function Field Name Description
RequestFields(...) Multiple Request ID, method, path, status, duration
CorrelationID(id) correlation_id Distributed tracing ID
ProjectID(id) project_id Project identifier
TokenID(token) token_id Token (auto-obfuscated)
ClientIP(ip) client_ip Client IP address

Security: TokenID() automatically obfuscates tokens to prevent sensitive data leakage.

Context Propagation

flowchart LR
    subgraph Request Flow
        R[Request] --> M[Middleware]
        M --> H[Handler]
        H --> S[Service]
    end
    
    subgraph Context
        C[Context with IDs]
    end
    
    M -->|WithRequestID| C
    M -->|WithCorrelationID| C
    C -->|GetRequestID| H
    C -->|GetCorrelationID| S

Key Functions:

  • WithRequestID(ctx, id) / GetRequestID(ctx) - Add/retrieve request ID
  • WithCorrelationID(ctx, id) / GetCorrelationID(ctx) - Add/retrieve correlation ID
  • WithRequestContext(ctx, logger) - Enrich logger with request ID from context
  • WithCorrelationContext(ctx, logger) - Enrich logger with correlation ID from context
  • NewChildLogger(parent, component) - Create component-specific logger

Output Formats

JSON Format (Production)
{
    "ts": "2024-01-15T10:30:45.123Z",
    "level": "info",
    "msg": "Request completed",
    "request_id": "req-abc123",
    "method": "POST",
    "status_code": 200
}
Console Format (Development)
2024-01-15T10:30:45.123Z  info  Request completed  {"request_id": "req-abc123", "method": "POST"}

Integration Flow

sequenceDiagram
    participant Client
    participant Middleware
    participant Handler
    participant Logger
    
    Client->>Middleware: Request
    Middleware->>Middleware: WithRequestID(ctx)
    Middleware->>Handler: ctx with IDs
    Handler->>Logger: WithRequestContext(ctx)
    Logger->>Logger: Log with request_id
    Handler->>Middleware: Response
    Middleware->>Logger: RequestFields(...)
    Middleware->>Client: Response

Testing Guidance

  • Unit Tests: Use zaptest.NewLogger(t) for test-captured output
  • Assert Logs: Use zap/zaptest/observer to capture and verify log entries
  • Existing Tests: See logger_test.go for comprehensive examples
  • Audit Package - Security audit logging (separate from application logs)

Files

File Description
logger.go Logger creation, canonical fields, and context propagation

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClientIP

func ClientIP(ip string) zap.Field

ClientIP returns a field for client IP address

func CorrelationID

func CorrelationID(id string) zap.Field

CorrelationID returns a field for correlation ID

func GetCorrelationID

func GetCorrelationID(ctx context.Context) (string, bool)

GetCorrelationID retrieves the correlation ID from context

func GetRequestID

func GetRequestID(ctx context.Context) (string, bool)

GetRequestID retrieves the request ID from context

func NewChildLogger

func NewChildLogger(parent *zap.Logger, component string) *zap.Logger

NewChildLogger creates a child logger with a component field

func NewLogger

func NewLogger(level, format, filePath string) (*zap.Logger, error)

NewLogger creates a zap.Logger with the specified level, format, and optional file output. level can be debug, info, warn, or error. format can be json or console. If filePath is empty, logs are written to stdout.

func ProjectID

func ProjectID(id string) zap.Field

ProjectID returns a field for project ID

func RequestFields

func RequestFields(requestID, method, path string, statusCode, durationMs int) []zap.Field

RequestFields returns fields for HTTP request logging

func TokenID

func TokenID(token string) zap.Field

TokenID returns a field for token ID (obfuscated for security)

func WithCorrelationContext

func WithCorrelationContext(ctx context.Context, logger *zap.Logger) *zap.Logger

WithCorrelationContext adds correlation ID from context to logger if present

func WithCorrelationID

func WithCorrelationID(ctx context.Context, correlationID string) context.Context

WithCorrelationID adds a correlation ID to the context

func WithRequestContext

func WithRequestContext(ctx context.Context, logger *zap.Logger) *zap.Logger

WithRequestContext adds request ID from context to logger if present

func WithRequestID

func WithRequestID(ctx context.Context, requestID string) context.Context

WithRequestID adds a request ID to the context

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL