Log
Logging utilities for Go applications.
Overview
The log package provides high-level logging abstractions with support for multiple logging backends. It includes wrappers for both structured logging (slog) and Kubernetes-style logging (klog).
Subpackages
slog
Structured logging with asynchronous processing, multiple outputs, and daily rotation.
π Documentation
Features:
- Asynchronous log processing with queue-based buffering
- Multiple output destinations (stdout, stderr, file)
- Daily log file rotation
- Configurable log levels (Trace, Debug, Info, Warn, Error, Fatal)
- Caller information tracking
Quick Example:
import "github.com/common-library/go/log/slog"
slog.SetOutputToFile("/var/log/app.log", 7, 100, 100*1024*1024)
slog.Info("Application started")
defer slog.Flush()
klog
Kubernetes-style logging wrapper with structured and formatted logging support.
π Documentation
Features:
- Kubernetes ecosystem compatibility
- Structured logging (InfoS, ErrorS)
- Formatted logging (Infof, Errorf)
- Caller tracking for debugging
- Fatal logging with application exit
Quick Example:
import "github.com/common-library/go/log/klog"
klog.Info("Server started")
klog.InfoS("Request processed", "method", "GET", "path", "/api/users")
defer klog.Flush()
Choosing a Logger
| Feature |
slog |
klog |
| Async Processing |
β
|
β |
| File Rotation |
β
|
β |
| Kubernetes Integration |
β |
β
|
| Structured Logging |
β
|
β
|
| Performance |
High (async) |
Moderate (sync) |
| Use Case |
General apps |
K8s controllers |
Installation
go get -u github.com/common-library/go/log/slog
go get -u github.com/common-library/go/log/klog
Best Practices
- Always Flush - Call
Flush() before application exit
- Set Appropriate Levels - Use Debug/Trace for development, Info+ for production
- Structured Data - Prefer structured logging for machine parsing
- Caller Info - Enable in development, consider disabling in production for performance
- File Rotation - Configure appropriate retention for disk space management
Further Reading