A colored slog.Handler for Go, plus a CLI tool for pretty-printing JSON log streams. Produces output styled after humanlog.
Jan 02 15:04:05.000 |INFO| server started addr=:8080
Jan 02 15:04:05.123 |DEBU| incoming request method=GET path=/health
Jan 02 15:04:05.124 |WARN| slow query duration=1.2s table=users
Jan 02 15:04:05.200 |ERRO| connection failed err="dial tcp: refused" retries=3
Timestamp and level are colored by severity. Keys are green, values are muted, messages are white.
Use colorlog.Handler as a drop-in slog.Handler:
import (
"log/slog"
"os"
"github.com/lfsgroup/colorlog"
)
func main() {
handler := colorlog.NewHandler(os.Stderr, nil)
slog.SetDefault(slog.New(handler))
slog.Info("server started", "addr", ":8080")
slog.Warn("slow query", "duration", "1.2s", "table", "users")
slog.Error("connection failed", "err", err, "retries", 3)
}Pass slog.HandlerOptions to configure the minimum log level:
handler := colorlog.NewHandler(os.Stderr, &slog.HandlerOptions{
Level: slog.LevelDebug,
})Handler implements the full slog.Handler interface, including WithAttrs and WithGroup:
logger := slog.New(colorlog.NewHandler(os.Stderr, nil))
// Attach persistent fields
reqLogger := logger.With("request_id", "abc123", "user_id", 42)
reqLogger.Info("handled request", "status", 200)
// Group fields under a namespace
dbLogger := logger.WithGroup("db")
dbLogger.Info("query executed", "table", "users", "rows", 10)
// output: ... db.table=users db.rows=10The colorlog CLI reads JSON log lines from stdin and renders them with color. Use it to pretty-print the output of any program that logs structured JSON.
go install github.com/lfsgroup/colorlog/cmd/colorlog@latestPipe any JSON log stream into colorlog:
./myapp | colorlogkubectl logs my-pod | colorlogtail -f /var/log/app.log | colorlogNon-JSON lines are passed through unchanged, so mixed output is handled gracefully.
| Field | Description |
|---|---|
time |
RFC3339/RFC3339Nano timestamp |
level |
Log level (DEBUG, INFO, WARN, ERROR) |
msg or message |
Log message |
| anything else | Rendered as key=value attributes |
go get github.com/lfsgroup/colorlogRequires Go 1.21 or later (uses log/slog from the standard library).