Skip to content
---
v2Now a log/slog handler

Log lines people can read

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
main.go
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"
  • 0allocations to log five attributes, where v1 made 7
  • 1.5×faster than slog.TextHandler to log five attributes
  • 42×faster than v1 to drop a record below the level
  • 100%test coverage, and the standard library’s slogtest suite passes

Features

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

Less work per record than slog’s own handlers

Section titled “Less work per record than slog’s own handlers”

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.

Time per record, lower is better
Show as a table
Time per record: time and allocations, slog.TextHandler against v2
Scenarioslog.TextHandlerv2
Message only259.0 ns0 allocs164.5 ns0 allocs
5 attributes446.5 ns0 allocs302.5 ns0 allocs
10 attributes837.0 ns2 allocs508.5 ns1 alloc
Groups530.7 ns0 allocs334.0 ns0 allocs
5 attributes, 16 goroutines205.6 ns0 allocs168.9 ns0 allocs
Below the level3.2 ns0 allocs3.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.

v1
logger := golog.New()
logger.Info(ctx, fmt.Sprintf(
"user %s signed in from %s",
name, ip,
))
logger.Critical(ctx, "disk full")
v2
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")

Readable logs in five minutes

Install the module, hand slog.New a golog handler, and log. The guide walks through levels, groups and colour.