A flexible, feature-rich logging package for Go applications built on top of zerolog. This logger provides structured logging with multiple output formats, log levels, and convenient helper functions.
- Multiple logging modes (Debug, Pretty, Info, Production, Test, JSON)
- Structured logging with JSON output
- Pretty printing with colors for development
- Configurable log levels (Debug, Info, Warn, Error, Fatal, Disabled)
- Component-based logging
- Trace ID support for request tracking
- Thread-safe operations
- Customizable time formats
- Caller information (optional)
- Color output (can be disabled)
- Log rotation with compression
- File-based logging with size limits
- Level-based log splitting
- Global fields support
- Automatic old log cleanup
go get github.com/theblitlabs/gologger
package main
import (
"github.com/theblitlabs/gologger"
)
func main() {
// Initialize with pretty printing for development
gologger.InitWithMode(gologger.LogModePretty)
// Log some messages
gologger.Info("main", "Application started")
// Log with additional fields
gologger.Info("main", "User logged in", map[string]interface{}{
"user_id": "123",
"ip": "192.168.1.1",
})
// Log an error
err := someOperation()
if err != nil {
gologger.Error("main", err, "Operation failed")
}
}
package main
import (
"github.com/theblitlabs/gologger"
)
func main() {
// Initialize logging to a file with default settings:
// - 100MB max file size
// - 7 days retention
// - 5 backup files
// - Compression enabled
err := gologger.InitWithFile("/var/log/myapp/app.log")
if err != nil {
panic(err)
}
// Log as usual
gologger.Info("main", "Application started")
}
config := gologger.Config{
Level: gologger.LogLevelInfo,
Pretty: false,
TimeFormat: time.RFC3339,
CallerEnabled: true,
Output: &gologger.OutputConfig{
File: "/var/log/myapp/app.log",
MaxSize: 100, // 100MB
MaxAge: 7 * 24 * time.Hour, // 7 days
MaxBackups: 5, // Keep 5 old files
Compress: true, // Compress old files
SplitLevel: true, // Split logs by level
},
Fields: map[string]interface{}{
"app_name": "myapp",
"version": "1.0.0",
},
}
gologger.Init(config)
When SplitLevel
is enabled, logs will be written to separate files based on their level:
/var/log/myapp/app.log
(all logs)/var/log/myapp/app.error.log
(error and fatal only)/var/log/myapp/app.info.log
(info and above)/var/log/myapp/app.debug.log
(debug and above)
Add fields to all log entries:
config := gologger.Config{
// ... other config ...
Fields: map[string]interface{}{
"environment": "production",
"service": "auth-api",
},
}
logger := gologger.WithComponent("auth")
logger.Info().Str("user", "john").Msg("User authenticated")
// Add multiple fields
logger := gologger.WithFields(map[string]interface{}{
"request_id": "123",
"user_id": "456",
})
// Add a single field
logger := gologger.WithField("transaction_id", "789")
if err := criticalOperation(); err != nil {
gologger.Fatal("main", err, "Critical error occurred", map[string]interface{}{
"operation": "system_init",
})
// Program will exit after this
}
You can customize the logger using the Config
struct:
config := gologger.Config{
Level: gologger.LogLevelDebug,
Pretty: true,
TimeFormat: time.RFC3339,
CallerEnabled: true,
NoColor: false,
}
gologger.Init(config)
LogModeDebug
: Debug level with pretty printingLogModePretty
: Info level with pretty printingLogModeInfo
: Standard info level loggingLogModeProd
: Production mode (no colors, nano timestamps)LogModeTest
: Test mode (error level only)
logger := gologger.WithComponent("auth")
logger.Info().Str("user", "john").Msg("User authenticated")
logger := gologger.WithTraceID("request-123")
logger.Info().Msg("Processing request")
MIT License
Contributions are welcome! Please feel free to submit a Pull Request.