Repository navigation
Conversation
iainlane
force-pushed
the
fix-double-context-handler
branch
from
November 12, 2026 12:46
51cb594 to
a789b97
Compare
When a context has no logger, `FromContext` falls back to
`NewLogger(nil)`, which wraps `slog.Default().Handler()` in a
`Handler`. A program can install a `Handler` as the default so that
plain `slog.InfoContext` calls get the context's values too. Its `clog`
calls then log through two `Handler`s, and each one adds every value
from `WithValues` to the record. The JSON handler writes each key
twice:
{"level":"INFO","msg":"","a":"b","a":"b"}
Cloud Logging does not keep one of the two values. It concatenates
them: a service that adds `component=api` with `WithValues` logs
`"component": "apiapi"`, and queries and alerts on that field stop
matching.
The fallback is not the only way to nest two `Handler`s. A `Handler`
with a nil inner handler looks up the default handler on every call.
`WithAttrs` and `WithGroup` build a new `Handler` around the handler
returned by the inner handler's method, and that handler is a `Handler`
when the inner handler is one. Whether two `Handler`s are nested is
therefore known only when a method runs, not when the `Handler` is
constructed.
`Handler` now skips any `Handler` directly inside it when it looks up
its inner handler. Every method uses that lookup, so when `Handler`s
are nested directly, only the outermost one adds the values. A program
can make a `Handler` the default without duplicating its values.
Two `Handler`s separated by a different handler still each add the
values, because slog has no way to ask a handler for the handler that
it wraps. `Handler`'s documentation now says to use one `Handler`, as
the outermost handler.
iainlane
force-pushed
the
fix-double-context-handler
branch
from
November 12, 2026 13:15
a789b97 to
2edd9bf
Compare
iainlane
marked this pull request as ready for review
November 12, 2026 13:19
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
clogadds the values fromWithValuesto a record inHandler.Handle.FromContextfalls back toNewLogger(nil)when the context has no logger, andNewLogger(nil)wrapsslog.Default().Handler()in aHandler.That works when the default handler is a plain
sloghandler, asclog/gcp/initinstalls. A program can also make aHandlerthe default, so that plainslog.InfoContextcalls get the context's values as well:Its
clogcalls then go through twoHandlers, and each adds every context value to the record. The JSON handler writes each key twice:This is more than noise. Cloud Logging doesn't keep one of the two values: it concatenates them. A service that adds
component=apiwithWithValueslogs"component": "apiapi", and queries and alerts on that field stop matching.The fallback is not the only way to nest two
Handlers. AHandlerwith a nil inner handler, asclog.New(nil)builds, looks up the default handler on every call.WithAttrsandWithGroupbuild a newHandleraround the handler returned by the inner handler's method, and that handler is aHandlerwhen the inner handler is one. Whether twoHandlers are nested is therefore known only when a method runs, not when theHandleris constructed.What this changes
Handlerskips anyHandler(or*Handler) directly inside it when it looks up its inner handler. Every method uses that lookup, so whenHandlers are nested directly, only the outermost one adds the context values. A program can make aHandlerits default without duplicating values, and its plainslogcalls keep the context's values.Two
Handlers separated by a different handler still each add the values, becausesloghas no way to ask a handler for the handler that it wraps.Handler's documentation now says to use oneHandler, as the outermost handler.Testing
TestContextHandlerWrappedTwicechecks the line written by each of six calls:Handleraround aHandler;clogcall with aHandleras the default;clogcall with a*Handleras the default;slogcall with aHandleras the default;clog.New(nil)with aHandleras the default;clog.New(nil).With("x", "y")with aHandleras the default.All but the plain
slogcall fail without this change, with"a":"b"written twice. The plainslogcall passes either way, and checks that plainslogcalls keep the context's values.The test's cleanup also restores the
logpackage's output and flags.slog.SetDefaultredirects thelogpackage to the new handler, and restoring the previous default does not undo that, so later tests in the package would otherwise write their default-logger output to this test's buffer.go vet ./...andgo test ./...pass.