A structured logging library for Go, built on top of slog with automatic log rotation, environment awareness, and flexible configuration.
- Built on Go 1.21+
slogpackage - Automatic log rotation (powered by lumberjack)
- JSON and text output formats
- Auto-naming log files based on program name
- Environment-aware configuration (test/production)
- Environment variables support
- Console and file output support
- Dynamic log level adjustment via signals
- Structured logging with field support
go get github.com/luojiego/slogxpackage main
import (
"fmt"
slogx "github.com/luojiego/slogx"
)
func main() {
// Use package-level functions
slogx.Info("Application started")
slogx.Debug("Debug information")
slogx.Error("Error occurred", "error", fmt.Errorf("321"))
// Use With to add extra fields
logger := slogx.With("module", "user-service")
logger.Info("User logged in", "userId", 123)
}- Log file location:
./logs/<program-name>.log - Log level: Debug
- Output format: Text
- Single log file size: 50MB
- Number of backup files: 100
- Log retention days: 30 days
Configure logging behavior through environment variables:
| Variable | Description | Default |
|---|---|---|
| LOG_MAX_SIZE | Maximum size of each log file (MB) | 50 |
| LOG_MAX_BACKUPS | Maximum number of old log files | 100 |
| LOG_MAX_AGE | Days to retain old log files | 30 |
| GO_ENV | Runtime environment (production/prod for production) | - |
Test Environment (Default):
- Outputs to both console and file
- No compression for old log files
Production Environment (GO_ENV=production/prod):
- File output only
- Automatic compression for old log files
Use NewLogger function for custom configuration:
logger := slogx.NewLogger(slogx.Config{
Level: "debug",
Format: "json",
Filename: "custom.log",
MaxSize: 100, // MB
MaxBackups: 10, // number of files
MaxAge: 7, // days
Compress: true, // compress old files
Stdout: true, // console output
})
// Set as default logger (optional)
slogx.SetDefaultLogger(logger)Adjust log levels at runtime using system signals:
SIGHUP: Set to Debug levelSIGUSR1: Set to Info levelSIGUSR2: Set to Warn level
Example (Unix/Linux):
# Switch to Debug level
kill -HUP <pid>
# Switch to Info level
kill -USR1 <pid>
# Switch to Warn level
kill -USR2 <pid>- Go 1.21+
- gopkg.in/natefinch/lumberjack.v2
MIT License
Issues and Pull Requests are welcome!