// FILE: config.go package log import ( "strings" "time" ) // Config holds all logger configuration values type Config struct { // Basic settings Level int64 `toml:"level"` Name string `toml:"name"` // Base name for log files Directory string `toml:"directory"` Format string `toml:"format"` // "txt", "raw", or "json" Extension string `toml:"extension"` // Formatting ShowTimestamp bool `toml:"show_timestamp"` ShowLevel bool `toml:"show_level"` TimestampFormat string `toml:"timestamp_format"` // Time format for log timestamps // Buffer and size limits BufferSize int64 `toml:"buffer_size"` // Channel buffer size MaxSizeMB int64 `toml:"max_size_mb"` // Max size per log file MaxTotalSizeMB int64 `toml:"max_total_size_mb"` // Max total size of all logs in dir MinDiskFreeMB int64 `toml:"min_disk_free_mb"` // Minimum free disk space required // Timers FlushIntervalMs int64 `toml:"flush_interval_ms"` // Interval for flushing file buffer TraceDepth int64 `toml:"trace_depth"` // Default trace depth (0-10) RetentionPeriodHrs float64 `toml:"retention_period_hrs"` // Hours to keep logs (0=disabled) RetentionCheckMins float64 `toml:"retention_check_mins"` // How often to check retention // Disk check settings DiskCheckIntervalMs int64 `toml:"disk_check_interval_ms"` // Base interval for disk checks EnableAdaptiveInterval bool `toml:"enable_adaptive_interval"` // Adjust interval based on log rate EnablePeriodicSync bool `toml:"enable_periodic_sync"` // Periodic sync with disk MinCheckIntervalMs int64 `toml:"min_check_interval_ms"` // Minimum adaptive interval MaxCheckIntervalMs int64 `toml:"max_check_interval_ms"` // Maximum adaptive interval // Heartbeat configuration HeartbeatLevel int64 `toml:"heartbeat_level"` // 0=disabled, 1=proc only, 2=proc+disk, 3=proc+disk+sys HeartbeatIntervalS int64 `toml:"heartbeat_interval_s"` // Interval seconds for heartbeat // Stdout/console output settings EnableStdout bool `toml:"enable_stdout"` // Mirror logs to stdout/stderr StdoutTarget string `toml:"stdout_target"` // "stdout" or "stderr" DisableFile bool `toml:"disable_file"` // Disable file output entirely // Internal error handling InternalErrorsToStderr bool `toml:"internal_errors_to_stderr"` // Write internal errors to stderr } // defaultConfig is the single source for all configurable default values var defaultConfig = Config{ // Basic settings Level: LevelInfo, Name: "log", Directory: "./logs", Format: "txt", Extension: "log", // Formatting ShowTimestamp: true, ShowLevel: true, TimestampFormat: time.RFC3339Nano, // Buffer and size limits BufferSize: 1024, MaxSizeMB: 10, MaxTotalSizeMB: 50, MinDiskFreeMB: 100, // Timers FlushIntervalMs: 100, TraceDepth: 0, RetentionPeriodHrs: 0.0, RetentionCheckMins: 60.0, // Disk check settings DiskCheckIntervalMs: 5000, EnableAdaptiveInterval: true, EnablePeriodicSync: true, MinCheckIntervalMs: 100, MaxCheckIntervalMs: 60000, // Heartbeat settings HeartbeatLevel: 0, HeartbeatIntervalS: 60, // Stdout settings EnableStdout: false, StdoutTarget: "stdout", DisableFile: false, // Internal error handling InternalErrorsToStderr: false, } // DefaultConfig returns a copy of the default configuration func DefaultConfig() *Config { // Create a copy to prevent modifications to the original return defaultConfig.Clone() } // Clone creates a deep copy of the configuration func (c *Config) Clone() *Config { copiedConfig := *c return &copiedConfig } // Validate performs validation on the configuration func (c *Config) Validate() error { // String validations if strings.TrimSpace(c.Name) == "" { return fmtErrorf("log name cannot be empty") } if c.Format != "txt" && c.Format != "json" && c.Format != "raw" { return fmtErrorf("invalid format: '%s' (use txt, json, or raw)", c.Format) } if strings.HasPrefix(c.Extension, ".") { return fmtErrorf("extension should not start with dot: %s", c.Extension) } if strings.TrimSpace(c.TimestampFormat) == "" { return fmtErrorf("timestamp_format cannot be empty") } if c.StdoutTarget != "stdout" && c.StdoutTarget != "stderr" && c.StdoutTarget != "split" { return fmtErrorf("invalid stdout_target: '%s' (use stdout, stderr, or split)", c.StdoutTarget) } // Numeric validations if c.BufferSize <= 0 { return fmtErrorf("buffer_size must be positive: %d", c.BufferSize) } if c.MaxSizeMB < 0 || c.MaxTotalSizeMB < 0 || c.MinDiskFreeMB < 0 { return fmtErrorf("size limits cannot be negative") } if c.FlushIntervalMs <= 0 || c.DiskCheckIntervalMs <= 0 || c.MinCheckIntervalMs <= 0 || c.MaxCheckIntervalMs <= 0 { return fmtErrorf("interval settings must be positive") } if c.TraceDepth < 0 || c.TraceDepth > 10 { return fmtErrorf("trace_depth must be between 0 and 10: %d", c.TraceDepth) } if c.RetentionPeriodHrs < 0 || c.RetentionCheckMins < 0 { return fmtErrorf("retention settings cannot be negative") } if c.HeartbeatLevel < 0 || c.HeartbeatLevel > 3 { return fmtErrorf("heartbeat_level must be between 0 and 3: %d", c.HeartbeatLevel) } // Cross-field validations if c.MinCheckIntervalMs > c.MaxCheckIntervalMs { return fmtErrorf("min_check_interval_ms (%d) cannot be greater than max_check_interval_ms (%d)", c.MinCheckIntervalMs, c.MaxCheckIntervalMs) } if c.HeartbeatLevel > 0 && c.HeartbeatIntervalS <= 0 { return fmtErrorf("heartbeat_interval_s must be positive when heartbeat is enabled: %d", c.HeartbeatIntervalS) } return nil }