A customizable and colorful handler for Go's slog designed for structured, readable, and beautiful terminal output.
- 🌈 Colored log levels (DEBUG, INFO, WARN, ERROR)
- 📝 Structured attributes with clean formatting
- 📂 Smart source tracking (file:line)
- 🪄 Multiline mode for complex data
- ⏱️ Custom timestamp formatting
go get github.com/Marlliton/slogprettySet up slogpretty as the default handler:
package main
import (
"log/slog"
"os"
"github.com/Marlliton/slogpretty"
)
func main() {
// Minimal setup
handler := slogpretty.New(os.Stdout, nil)
slog.SetDefault(slog.New(handler))
slog.Info("Server started", "port", 8080)
}You can configure the handler using the Options struct:
handler := slogpretty.New(os.Stdout, &slogpretty.Options{
Level: slog.LevelDebug,
AddSource: true, // Show file location
Colorful: true, // Enable colors. Default is true
Multiline: true, // Pretty print for complex data
TimeFormat: slogpretty.DefaultTimeFormat, // Custom format (e.g., time.Kitchen)
})-
Colored Levels
Each log level has a distinct color.
-
Structured Attributes
Clean formatting for attributes.
slog.Debug("Debugging data")
slog.Info("Informational message")
slog.Warn("Potential issue detected")
slog.Error("Operation failed")
slog.Info("User logged in", "user_id", 1234, "email", "[email protected]", "active", true)- Complex Data in Multiple Lines
slog.Info("Event with group and subgroups",
"user", "bob",
slog.Group("details",
slog.Int("port", 8080),
slog.String("status", "inactive"),
slog.Group("metrics",
slog.Float64("cpu", 72.5),
slog.Float64("memory", 65.3),
),
slog.Group("location",
slog.String("country", "Brazil"),
slog.String("region", "SP"),
slog.Group("coordinates",
slog.Float64("lat", -23.5505),
slog.Float64("lon", -46.6333),
),
),
),
"session", "0x93AF21",
"authenticated", false,
)In development environments, it's recommended to enable as many features as possible to improve log readability, debugging, and traceability:
&pretty.Options{
Level: slog.LevelDebug,
AddSource: true,
Colorful: true,
Multiline: true,
}These options provide:
- Logs from the debug level (
LevelDebug) - Source tracking (
AddSource) - Colored output for better terminal visibility (
Colorful) - Structured multiline formatting (
Multiline)
For production, it's best to use slog.NewJSONHandler, which generates logs in JSON format:
JSONhandler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelInfo,
})
slog.SetDefault(slog.New(JSONhandler))
slog.Info("Event with groups and subgroups",
"user", "bob",
slog.Group("details",
slog.Int("port", 8080),
slog.String("status", "inactive"),
slog.Group("metrics",
slog.Float64("cpu", 72.5),
slog.Float64("memory", 65.3),
),
slog.Group("location",
slog.String("country", "Brazil"),
slog.String("region", "SP"),
slog.Group("coordinates",
slog.Float64("lat", -23.5505),
slog.Float64("lon", -46.6333),
),
),
),
"session", "0x93AF21",
"authenticated", false,
)- Compatible with observability tools (e.g., Datadog, Loki, Grafana)
- Widely accepted format — simplifies analysis, searching, etc.
- Standardized structure — ideal for distributed systems and centralized logging
- Efficient — avoids overhead from unnecessary visual formatting




