Documentation
¶
Overview ¶
Package logger provides a buffered, rotating logger with production-ready features including automatic file rotation, disk space management, and dropped log detection.
Features: - Buffered asynchronous logging with configurable capacity - Automatic log file rotation based on size - Disk space management with configurable limits:
- Maximum total log directory size
- Minimum required free disk space
- Automatic cleanup of old logs when limits are reached
- Dropped logs detection and reporting - Unique timestamp-based log file naming with nanosecond precision - Thread-safe operations using atomic counters - Context-aware logging with cancellation support - Multiple log levels (Debug, Info, Warn, Error) matching slog levels - Efficient JSON and TXT structured logging with zero allocations - Function call trace support with configurable depth - Graceful shutdown with context support - Runtime reconfiguration - Disk full protection with logging pause - Log retention management with configurable period and check interval
Lixen Wraith, 2024
Index ¶
- Constants
- func Config(cfg *LoggerConfig) error
- func Debug(logCtx context.Context, args ...any)
- func DebugTrace(logCtx context.Context, depth int, args ...any)
- func EnsureInitialized() bool
- func Error(logCtx context.Context, args ...any)
- func ErrorTrace(logCtx context.Context, depth int, args ...any)
- func Info(logCtx context.Context, args ...any)
- func InfoTrace(logCtx context.Context, depth int, args ...any)
- func Init(ctx context.Context, cfg ...*LoggerConfig) error
- func LogWithFlags(ctx context.Context, flags int64, level int64, depth int64, args ...any)
- func Shutdown(ctx ...context.Context) error
- func Warn(logCtx context.Context, args ...any)
- func WarnTrace(logCtx context.Context, depth int, args ...any)
- type LoggerConfig
Constants ¶
const ( LevelDebug int64 = -4 // matches slog.LevelDebug LevelInfo int64 = 0 // matches slog.LevelInfo LevelWarn int64 = 4 // matches slog.LevelWarn LevelError int64 = 8 // matches slog.LevelError )
Log level constants match slog levels for consistency with applications that use it. These values are used to determine which logs to write based on minimum level configuration.
const ( // Record flags for controlling output structure FlagShowTimestamp int64 = 0b01 FlagShowLevel int64 = 0b10 FlagDefault = FlagShowTimestamp | FlagShowLevel )
Variables ¶
This section is empty.
Functions ¶
func Config ¶
func Config(cfg *LoggerConfig) error
Config initializes the logger with the provided configuration.
func Debug ¶
Debug logs a message at debug level with the given context and additional arguments. Messages are dropped if the logger's level is higher than debug or if logger is not initialized.
func DebugTrace ¶
DebugTrace is Debug log with trace.
func EnsureInitialized ¶ added in v1.2.0
func EnsureInitialized() bool
EnsureInitialized checks if the logger is initialized, and initializes if not. returns true if it was already initialized or initialization attempt was successful. returns false if logger cannot be initialized.
func Error ¶
Error logs a message at error level with the given context and additional arguments. Messages are dropped if the logger's level is higher than error or if logger is not initialized.
func ErrorTrace ¶
ErrorTrace is Error log with trace.
func Info ¶
Info logs a message at info level with the given context and additional arguments. Messages are dropped if the logger's level is higher than info or if logger is not initialized.
func Init ¶
func Init(ctx context.Context, cfg ...*LoggerConfig) error
Init initializes the logger with the provided configuration and context.
func LogWithFlags ¶ added in v1.2.1
LogWithFlags allows custom flag control for logging with specified flags, level and trace depth
func Shutdown ¶
Shutdown gracefully shuts down the logger, ensuring all buffered messages are written and files are properly closed. It respects context cancellation for timeout control.
Types ¶
type LoggerConfig ¶
type LoggerConfig struct { Level int64 `json:"level" toml:"level"` // LevelDebug, LevelInfo, LevelWarn, LevelError Name string `json:"name" toml:"name"` // Base name for log files Directory string `json:"directory" toml:"directory"` // Directory to store log files Format string `json:"format" toml:"format"` // Serialized output file type: txt, json Extension string `json:"extension" toml:"extension"` // Log file extension (default "log", empty = use format) ShowTimestamp bool `json:"show_timestamp" toml:"show_timestamp"` // Enable time stamp (default enabled) ShowLevel bool `json:"show_level" toml:"show_level"` // Enable level (default enabled) BufferSize int64 `json:"buffer_size" toml:"buffer_size"` // Channel buffer size MaxSizeMB int64 `json:"max_size_mb" toml:"max_size_mb"` // Max size of each log file in MB MaxTotalSizeMB int64 `json:"max_total_size_mb" toml:"max_total_size_mb"` // Max total size of the log folder in MB to trigger old log deletion/pause logging MinDiskFreeMB int64 `json:"min_disk_free_mb" toml:"min_disk_free_mb"` // Min available free space in MB to trigger old log deletion/pause logging FlushTimer int64 `json:"flush_timer" toml:"flush_timer"` // Periodically forces writing logs to the disk to avoid missing logs on program shutdown TraceDepth int64 `json:"trace_depth" toml:"trace_depth"` // 0-10, 0 disables tracing RetentionPeriod float64 `json:"retention_period" toml:"retention_period"` // RetentionPeriod defines how long to keep log files in hours. Zero disables retention. RetentionCheckInterval float64 `json:"retention_check_interval" toml:"retention_check_interval"` // RetentionCheckInterval defines how often to check for expired logs in minutes if retention is enabled. }
LoggerConfig defines the logger configuration parameters. All fields can be configured via JSON or TOML configuration files.
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
examples
|
|
goroutine
goroutine: the program demonstrates logger usage in goroutines with context timeout
|
goroutine: the program demonstrates logger usage in goroutines with context timeout |
quick
quick: the program demonstrates logger/quick interface usage
|
quick: the program demonstrates logger/quick interface usage |
quick_goroutine
simplified_goroutine: the program demonstrates simplified interface usage with goroutines
|
simplified_goroutine: the program demonstrates simplified interface usage with goroutines |
spot_traced
spot_traced: program demonstrates switching between traced and untraced logs
|
spot_traced: program demonstrates switching between traced and untraced logs |
traced
traced: the program demonstrates logger and logger/quick trace usage
|
traced: the program demonstrates logger and logger/quick trace usage |