Skip to content

Configuration

DD uses struct-based configuration (Config) with IDE autocompletion support — no builder chains or option functions needed. This guide covers all configuration fields and common combinations.

API Reference: See Config for the complete field list.

Three Presets

PresetLevelFormatSecurity FilterTypical Use
DefaultConfig()InfoText✅ BasicProduction default
DevelopmentConfig()DebugText (short time)✅ BasicLocal development
JSONConfig()DebugJSON (RFC3339)✅ BasicLog aggregation systems

Security filter enabled by default

All three presets enable basic sensitive-data filtering (passwords, API keys, credit cards, etc.). Even development mode keeps it on — to catch accidental sensitive-data leaks early. To disable, explicitly set Security: &dd.SecurityConfig{} or use SecurityLevelDevelopment.

Config Field Overview

go
type Config struct {
    // ─── Basic ───
    Level         LogLevel     // Log level (LevelDebug ~ LevelFatal)
    Format        LogFormat    // Output format (FormatText / FormatJSON)
    TimeFormat    string       // Time format (default ISO 8601)
    IncludeTime   bool         // Include timestamp
    IncludeLevel  bool         // Include log level

    // ─── Caller Info ───
    DynamicCaller bool         // Dynamic caller detection (file:line)
    FullPath      bool         // Use full file path (default: filename only)

    // ─── Output Targets ───
    Targets       []OutputTarget // Output destinations (ConsoleOutput/FileOutput/CustomOutput)

    // ─── Format Customization ───
    JSON          *JSONOptions // JSON format options (field names, indent, etc.)

    // ─── Security ───
    Security      *SecurityConfig       // Security config (filtering, rate limiting)
    FieldValidation *FieldValidationConfig // Field key naming validation

    // ─── Lifecycle ───
    FatalHandler      FatalHandler      // Custom fatal handler
    WriteErrorHandler WriteErrorHandler // Write-error callback

    // ─── Extensions ───
    ContextExtractors []ContextExtractor // Context field extractors
    Hooks             *HookRegistry      // Lifecycle hooks
    Sampling          *SamplingConfig    // Log sampling

    // ─── Audit ───
    Audit             *AuditConfig       // Security audit logging
}

Output Target Configuration

Use ConsoleOutput(), FileOutput(), CustomOutput() to construct output targets:

go
package main

import (
    "log"
    "os"

    "github.com/cybergodev/dd"
)

func main() {
    cfg := dd.DefaultConfig()
    cfg.Targets = []dd.OutputTarget{
        dd.ConsoleOutput(),                          // Console
        dd.FileOutput("logs/app.log"),               // File (default 100MB/10 backups/30 days)
        dd.CustomOutput(os.Stderr),                  // Custom Writer
    }

    // Custom file rotation parameters
    fileTarget := dd.FileOutput("logs/app.log")
    fileTarget.MaxSizeMB = 50     // Rotate at 50MB
    fileTarget.MaxBackups = 5     // Keep 5 backups
    fileTarget.MaxAge = 7 * 24    // Retain 7 days

    logger, err := dd.New(cfg)
    if err != nil {
        log.Fatal(err)
    }
    defer logger.Close()
}

Zero-value Config pitfall

Using dd.Config{Targets: ...} directly misses timestamps, level, and caller info. Always start from dd.DefaultConfig() and modify fields.

JSON Format Customization

go
cfg := dd.JSONConfig()

// Custom JSON field names
cfg.JSON = &dd.JSONOptions{
    PrettyPrint: true,
    Indent:      "  ",
    FieldNames: &dd.JSONFieldNames{
        Timestamp: "@timestamp",
        Level:     "severity",
        Message:   "msg",
        Caller:    "source",
        Fields:    "ctx",
    },
}

Security Configuration

go
cfg := dd.DefaultConfig()

// Option A: By security level (recommended)
cfg.Security = dd.SecurityConfigForLevel(dd.SecurityLevelStandard)

// Option B: Industry presets
cfg.Security = dd.HealthcareConfig()   // HIPAA
cfg.Security = dd.FinancialConfig()    // PCI-DSS
cfg.Security = dd.GovernmentConfig()   // Government

// Option C: Custom
cfg.Security = &dd.SecurityConfig{
    MaxMessageSize: 1024 * 1024, // 1MB
    SensitiveFilter: dd.NewSensitiveDataFilter(),
}

See Sensitive Data Filtering and Security Overview.

Field Validation

go
cfg := dd.DefaultConfig()
cfg.FieldValidation = dd.StrictSnakeCaseConfig()
// All field keys must be snake_case, otherwise stderr warning

See Field Validation.

Log Sampling

go
cfg := dd.DefaultConfig()
cfg.Sampling = &dd.SamplingConfig{
    Enabled:    true,
    Initial:    100,            // First 100 entries always logged
    Thereafter: 10,             // Then log 1 out of every 10
    Tick:       time.Second,    // Reset counter every second
}

See Log Sampling.

Hooks and Audit

go
// Hooks
registry := dd.NewHooksFromConfig(dd.HooksConfig{
    AfterLog: []dd.Hook{func(ctx context.Context, hc *dd.HookContext) error {
        // Send to metrics system
        return nil
    }},
})
cfg.Hooks = registry

// Audit
cfg.Audit = &dd.AuditConfig{
    Enabled:     true,
    Output:      auditFile,
    JSONFormat:  true,
    MinimumSeverity: dd.AuditSeverityWarning,
}

See Hook System and Audit Logging.

Clone: Configuration Deep Copy

Clone() creates a configuration copy, useful for deriving different configs from the same base:

go
base := dd.DefaultConfig()
base.Format = dd.FormatJSON

// Derivation 1: Production config
prodCfg := base.Clone()
prodCfg.Level = dd.LevelInfo
prodCfg.Targets = []dd.OutputTarget{dd.FileOutput("logs/prod.log")}

// Derivation 2: Debug config
debugCfg := base.Clone()
debugCfg.Level = dd.LevelDebug
debugCfg.Targets = []dd.OutputTarget{dd.ConsoleOutput()}

// base is unaffected

Clone copy depth

Deep copy: JSON, Security, Hooks, Sampling, Audit, Targets slice. Shallow copy: FatalHandler, WriteErrorHandler, FieldValidation (functions/pointers shared). ContextExtractors slice copied but extractor instances shared.

Next Steps