A colorful, readable logging handler for Go's standard slog package. Makes logs easier to read during development with color-coded levels and clean formatting. Inspired by zerolog's console output.
- 🎨 Color-coded levels: DEBUG, INFO, WARN, ERROR
- 🔍 Bold messages: Highlights log messages and errors
- 🧩 Field formatting: Formats fields based on type
- ⏱ Custom time: Override the default
time.Kitchenformat - 🚫 No-color mode: Disable ANSI colors
- 🖌️ Custom colors: Set your own colors per log level
go get github.com/nentgroup/slog-prettyloggerpackage main
import (
"log/slog"
"os"
"time"
"github.com/nentgroup/slog-prettylogger"
)
func main() {
// Create a new pretty logger
logger := slog.New(prettylogger.NewHandler(os.Stdout, prettylogger.HandlerOptions{
SlogOpts: slog.HandlerOptions{
AddSource: true, // Include caller information
Level: slog.LevelDebug,
},
TimeFormat: time.TimeOnly, // Customize time format, default is time.Kitchen
}))
// Set as default logger
slog.SetDefault(logger)
// Basic logging
slog.Info("application started", "version", "1.0.0")
slog.Debug("debug information", "cache_hits", 42)
slog.Warn("resource usage high", "cpu", 85.5, "memory", "3.2GB")
slog.Error("failed to connect to database",
"error", "connection refused",
"retry_count", 3,
"db_host", "localhost:5432")
// With structured data
logger.Info("user logged in",
"user_id", 123,
"metadata", map[string]interface{}{
"browser": "Chrome",
"version": "98.0.4758.102",
"platform": "macOS",
})
}When using pretty logger, your console output will look similar to:
Use the HandlerOptions struct to customize the logger:
type HandlerOptions struct {
SlogOpts slog.HandlerOptions // Standard slog handler options (level, AddSource, etc.)
TimeFormat string // Optional: custom time format (default is time.Kitchen)
NoColor bool // Optional: disable ANSI colors
LevelColors map[slog.Level]string // Optional: override colors per log level (DEBUG, INFO, WARN, ERROR)
}logger := slog.New(prettylogger.NewHandler(os.Stdout, prettylogger.HandlerOptions{
SlogOpts: slog.HandlerOptions{
AddSource: true,
Level: slog.LevelDebug,
},
TimeFormat: time.RFC3339,
LevelColors: map[slog.Level]string{slog.LevelError: prettylogger.BoldRed}, // or use any ANSI color code
}))Contributions are welcome! Please feel free to submit a Pull Request.
Developed and maintained by Viaplay Group.

