A structured logging package for Go applications built on top of the standard library's slog package.
- Built on Go's standard library: Uses
slogfrom Go 1.21+ - Structured logging: Key-value pairs for better log filtering and analysis
- Multiple output formats: JSON for production, text for development
- Colored console output: Improves readability during local development
- Customizable log levels: debug, info, warn, error
- Source information: Automatically includes caller information (file:line)
- Global logger: Convenient access throughout your application
- Testing support: Mock logger for easy testing
go get github.com/legrch/loggerpackage main
import (
"github.com/legrch/logger"
"os"
)
func main() {
// Initialize logger with default configuration
cfg := &logger.Config{
Level: "info",
Format: "text",
EnableColors: true,
EnableCaller: true,
CallerSkipFrame: 1,
}
log := logger.New(cfg)
// Use the logger
log.Info("application started", "version", "1.0.0")
log.Debug("debug message") // Won't be shown with info level
log.Error("something went wrong", "error", "connection refused", "retry", true)
// With structured fields
log.With("request_id", "abc123").Info("processing request")
// Set as global logger
logger.SetGlobal(log)
// Use global logger elsewhere
globalLogger := logger.Global()
globalLogger.Info("using global logger")
}For detailed documentation, examples, and API reference, please visit:
This project is licensed under the MIT License - see the LICENSE file for details.