3 ms·
I think Uber chose a better approach for their Go logging library called Zap [1] logger.Info("failed to fetch URL", // Structured context as strongly
by jupp0r 3y ago
I think Uber chose a better approach for their Go logging library called Zap [1]
logger.Info("failed to fetch URL",
// Structured context as strongly typed Field values.
zap.String("url", url),
zap.Int("attempt", 3),
zap.Duration("backoff", time.Second),
)
They also have zap.Error(err) which generates the "error" key as a convention.
[1] https://github.com/uber-go/zap https://github.com/uber-go/zap
- bpicolo 3y agoAs mentioned in the article, Zap's SugaredLogger has roughly the same thing https://pkg.go.dev/go.uber.org/zap https://pkg.go.dev/go.uber.org/zap sugar.Infow("failed to fetch URL", "url", "http://example.com", "attempt", 3, "backoff", time.Second, ) Really slog presents mostly the same two APIs as Zap, with a single entrypoint.
- Groxx 3y ago`[level]w` supports both key/value pairs and individual `zap.Type(key, value)` values combined fwiw, so for that part the API is essentially the same too
- masklinn 3y agoSlog allows doing that by creating Attr structs directly.
- sa46 3y agoYou can reproduce that example with slog verbatim by changing zap to slog. For example: https://pkg.go.dev/golang.org/x/exp/slog#example-Group https://pkg.go.dev/golang.org/x/exp/slog#example-Group