log Module
The log module provides structured logging powered by Go's slog. It supports levels, structured fields, persistent context, and file rotation in text or JSON format.
Overview
The log module offers:
- Multiple log levels: DEBUG, INFO, WARN, ERROR
- Structured logging: Key-value pairs for machine-readable logs
- Formatted logging: Printf-style formatting with placeholders
- Level management: Set and query current log level
- File output: Write logs to rotating files
- Rotation: Automatic log rotation by size, age, and backup count
- Multiple formats: Text and JSON output
- Persistent context: Add fields that persist across log calls
Functions
Info(values...)
Logs at INFO level. If arguments are key/value pairs (string key), they are logged as structured fields. Otherwise, they are joined into a message.
Returns: - (None, error)
Example:
| Info Log | |
|---|---|
Warn(values...)
Logs at WARN level.
Returns: - (None, error)
Example:
Error(values...)
Logs at ERROR level.
Returns: - (None, error)
Example:
Debug(values...)
Logs at DEBUG level.
Returns: - (None, error)
Example:
SetLevel(level)
Sets the current log level. Supported values: DEBUG, INFO, WARN, ERROR.
Parameters: - level (string)
Returns: - (None, error)
Example:
| Set Log Level | |
|---|---|
SetOutput(path)
Directs logs to a rotating file with text format using defaults: 10MB max size, 5 backups, 7 days retention, no compression.
Parameters: - path (string): Log file path.
Returns: - (None, error)
Example:
ConfigureRotation(format, path, maxSizeMB, maxBackups, maxAgeDays, compress)
Configures rotation and output format.
Parameters: - format (string): text or json. - path (string): Log file path. - maxSizeMB (int): Max file size in MB before rotation. - maxBackups (int): Number of rotated files to keep. - maxAgeDays (int): Delete files older than this many days (0 disables). - compress (boolean): Gzip rotated files.
Returns: - (None, error)
Example:
| Set Log Handler | |
|---|---|
With(key, value)
Adds a persistent structured attribute to the logger. Subsequent logs include this field.
Parameters: - key (string) - value (any)
Returns: - (None, error)
Example:
| Log With Fields | |
|---|---|
Printf(format, args...)
Logs a formatted message at INFO level using fmt-style formatting with placeholders like %s, %d, %v, %f, etc.
Parameters: - format (string): Format string with placeholders - args... (any): Values to substitute into format string
Returns: - (None, error)
Example:
| Formatted Logging | |
|---|---|
GetLevel()
Returns the current logging level as a string.
Returns: - (string, error): Current level (DEBUG, INFO, WARN, or ERROR)
Example:
Quick Reference
| Function | Purpose | Returns |
|---|---|---|
Info(values...) | Log at INFO level | (None, error) |
Warn(values...) | Log at WARN level | (None, error) |
Error(values...) | Log at ERROR level | (None, error) |
Debug(values...) | Log at DEBUG level | (None, error) |
Printf(format, args...) | Formatted logging at INFO level | (None, error) |
SetLevel(level) | Set current log level | (None, error) |
GetLevel() | Get current log level | (string, error) |
SetOutput(path) | Direct logs to file with rotation | (None, error) |
ConfigureRotation(...) | Configure rotation and format | (None, error) |
With(key, value) | Add persistent structured field | (None, error) |