Lines for people
The time, a level that lines up, the message as it is, then key=value. Values are quoted only when they need
to be, and a record is always one line.
A handler for Go's log/slog that writes each record as one line, the time, level, message and key=value pairs, in colour for terminals and with no allocations per record.
go get github.com/vardius/golog/v2 logger := slog.New(golog.NewHandler( os.Stderr, &golog.Options{Color: true},))
logger.Info("server started", "addr", ":8080")logger.Error("payment failed", "order", 1042, "err", err)
// After the time, it prints:// INFO server started addr=:8080// ERROR payment failed order=1042// err="card declined"// With is formatted once, not per record.reqs := logger.With("svc", "api"). WithGroup("http")
reqs.Warn("slow request", "path", "/search", "took", took)
// After the time, it prints:// WARN slow request svc=api// http.path=/search http.took=1.204svar level slog.LevelVar // Infologger := slog.New(golog.NewHandler( os.Stderr, &golog.Options{Level: &level},))
// Safe while other goroutines log.level.Set(slog.LevelDebug)
logger.Log(ctx, golog.LevelCritical, "disk full", "free", 0)// CRITICAL disk full free=0Features
Lines for people
The time, a level that lines up, the message as it is, then key=value. Values are quoted only when they need
to be, and a record is always one line.
Colour for terminals
Levels in colour, the time and keys dimmed, errors in red. One option, off for files and pipes.
Standard slog
A slog.Handler: log with *slog.Logger, set levels with slog.LevelVar, rewrite with ReplaceAttr.
It passes the standard library’s testing/slogtest.
No allocations
Values go straight into a pooled buffer, With attributes are formatted once, and tests guard every
allocation count.
One write per record
Each line is one Write under a lock shared by every handler made from it, so lines from many goroutines never mix.
Small API
One constructor, four options and a CRITICAL level. No dependencies beyond the standard library.
Performance
golog writes fewer bytes than slog.TextHandler, quotes without going rune by rune, and formats the time in full once a second. Against v1, it allocates nothing and drops a record below the level before formatting it. With 16 goroutines at once, the time is wall time per record across all of them, so it measures throughput.
| Scenario | slog.TextHandler | v2 |
|---|---|---|
| Message only | 259.0 ns0 allocs | 164.5 ns0 allocs |
| 5 attributes | 446.5 ns0 allocs | 302.5 ns0 allocs |
| 10 attributes | 837.0 ns2 allocs | 508.5 ns1 alloc |
| Groups | 530.7 ns0 allocs | 334.0 ns0 allocs |
| 5 attributes, 16 goroutines | 205.6 ns0 allocs | 168.9 ns0 allocs |
| Below the level | 3.2 ns0 allocs | 3.6 ns0 allocs |
Apple M4 Max, Go 1.27.0, median of 5 runs.
| Scenario | slog.JSONHandler | v2 |
|---|---|---|
| Message only | 263.6 ns0 allocs | 164.5 ns0 allocs |
| 5 attributes | 445.2 ns0 allocs | 302.5 ns0 allocs |
| 10 attributes | 777.1 ns3 allocs | 508.5 ns1 alloc |
| Groups | 483.4 ns0 allocs | 334.0 ns0 allocs |
| 5 attributes, 16 goroutines | 208.7 ns0 allocs | 168.9 ns0 allocs |
| Below the level | 3.2 ns0 allocs | 3.6 ns0 allocs |
Apple M4 Max, Go 1.27.0, median of 5 runs.
| Scenario | v1 | v2 in colour |
|---|---|---|
| Message only | 113.3 ns1 alloc | 174.1 ns0 allocs |
| 5 attributes | 260.7 ns7 allocs | 310.4 ns0 allocs |
| 10 attributes | 490.4 ns10 allocs | 521.8 ns1 alloc |
| Groups | 314.8 ns7 allocs | 344.2 ns0 allocs |
| 5 attributes, 16 goroutines | 204.2 ns7 allocs | 169.5 ns0 allocs |
| Below the level | 149.4 ns6 allocs | 3.6 ns0 allocs |
Apple M4 Max, Go 1.27.0, median of 5 runs.
Upgrading
v2 is a handler for log/slog instead of a logger of its own. The calls become slog’s, the values become attributes, and the levels keep their names.
logger := golog.New()
logger.Info(ctx, fmt.Sprintf( "user %s signed in from %s", name, ip,))logger.Critical(ctx, "disk full")logger := slog.New(golog.NewHandler( os.Stdout, &golog.Options{Color: true},))logger.InfoContext(ctx, "user signed in", "user", name, "ip", ip)logger.Log(ctx, golog.LevelCritical, "disk full")Install the module, hand slog.New a golog handler, and log. The guide walks through levels, groups and colour.