Go 1.21 introduced the slog package as the standard structured logging solution. Master vocabulary for the key-value variadic API, typed attributes (slog.String/Int/etc.), the Handler interface and Record.Attrs iteration, child loggers via With(), Enabled() pre-checks, and JSONHandler for production use.
0 / 5 completed
1 / 5
A Go developer calls slog.Info("request", "method", r.Method, "path", r.URL.Path). How does slog interpret the alternating string/value arguments?
The slog.Info variadic args parameter accepts alternating key-value pairs: odd-indexed arguments are string keys, even-indexed are values. This is the 'loosely typed' API. For performance-critical code, prefer slog.String("method", r.Method) typed attributes to avoid interface boxing.
2 / 5
What is the performance benefit of using logger.Enabled(ctx, slog.LevelDebug) before calling logger.Debug(...)?
Calling Enabled first lets you short-circuit expensive argument construction when debug logging is disabled in production. Without the check, arguments are evaluated (including memory allocations, fmt.Sprintf calls, etc.) even if the message will be discarded by the handler.
3 / 5
A team implements a custom slog.Handler. The Handle method receives a slog.Record. How should they iterate over its attributes?
slog.Record.Attrs is a method that accepts a callbackfunc(slog.Attr) bool. Returning false stops iteration early. This design avoids allocating a slice for the common case where records have few attributes (they're stored inline in the Record struct up to a threshold).
4 / 5
A developer uses slog.With("requestID", id) to create a child logger. What does this return?
slog.With returns a new *slog.Logger that pre-attaches the specified attributes to every log record it emits. The original logger is unchanged. This is the idiomatic way to create request-scoped loggers with correlation IDs, user IDs, etc.
5 / 5
Which slog.Handler implementation should a developer choose for production JSON logging to stdout?
slog.NewJSONHandler outputs each log record as a single-line JSON object, which is the standard format for structured log ingestion by systems like Cloud Logging, Datadog, or ELK. Adding AddSource: true (option C) is useful in development but adds overhead in production. Option B is the clean production baseline.