logger: replace WithTap with WithTee - #1790
Open
paulwe wants to merge 4 commits into
Open
Conversation
Deferrer.write and Deferrer.flush called the wrapped core's Write directly, bypassing Check. zapcore.multiCore.Write fans out to every child unconditionally, so a destination that filters in Check rather than in Write received entries it never enabled. encoderCore was immune because it re-checks each out's level inside Write; nothing else is, including zaptest/observer. testCore now registers itself in Check. It overrides Write but not Check, so its Write only ever ran because the deferred path skipped Check.
🦋 Changeset detectedLatest commit: 613798c The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
WithTap took a WriteSyncer, so a tap could only ever receive bytes encoded by a zaputil-chosen encoder: Encoder.Core took two destinations and DevelopmentEncoder carried a second JSON encoder that existed only to format tap output. WithTee hands the duplicate stream a zapcore.Core instead, which brings its own encoder and sees the structured entry. A Tee builds its core from the derived logger's resolved enabler, so it shares the console's level rather than widening what the logger emits, and the component leveler no longer has to OR a tap's level in. Fields do not arrive on their own: WithValues bakes them into the encoder and replaces the whole core, and the sugared path never calls Core.With, so a Tee accumulates them itself. Benchmarks cover objects allocated per WithValues and WithDeferredValues, with and without a tee.
With one destination per core, encoderCore is zapcore.ioCore plus a per-destination level re-check on write, and that check became redundant once the deferred path started replaying through Check.
Encoder was generic because WithValues returned a concrete type, and there were two concrete types because DevelopmentEncoder needed a second encoder for the tap. Both are now one encoder and one destination, so the interface and its two implementations collapse into a struct with two constructors, and zapLogger, newZapLogger and zapLoggerComponentLeveler stop being generic.
paulwe
force-pushed
the
logger-with-tee
branch
from
September 9, 2026 15:17
6b5dc18 to
613798c
Compare
boks1971
approved these changes
Sep 9, 2026
cnderrauber
approved these changes
Sep 10, 2026
cnderrauber
left a comment
Contributor
There was a problem hiding this comment.
Looks good! Will wait for this PR to land
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.
WithTaptook aWriteSyncer, so a tap could only ever receive bytes encoded by a zaputil-chosen encoder —Encoder.Coretook two destinations, andDevelopmentEncodercarried a second JSON encoder that existed only to format tap output.WithTeehands the duplicate stream azapcore.Coreinstead, built per derived logger from that logger's resolved enabler, so it brings its own encoder and sees the structured entry.Four commits, each green on its own:
replay deferred writes through Check—Deferrer.write/flushcalled the wrapped core'sWritedirectly.zapcore.multiCore.Writefans out to every child unconditionally, so a destination that filters inCheckrather than inWritereceived entries it never enabled.encoderCorewas immune because it re-checks each out's level insideWrite; nothing else is, includingzaptest/observer.replace WithTap with WithTee— the API change, plus allocation benchmarks.replace encoderCore with zapcore.NewCore— with one destination per core,encoderCoreisioCoreplus the write-time level check that commit 1 made redundant.drop the Encoder type parameter— both encoders are now one encoder and one destination, so the interface and its two implementations collapse.Level semantics
A tee shares the console's resolved level and cannot widen what the logger emits. That is what lets the component leveler drop its
OrLevelEnablerand thelevelEnablerscache go away entirely.Allocations
WithValues/WithDeferredValuesrun per participant and track, sologger/logger_bench_test.gocovers objects allocated on both, with and without a tee. Every no-tee path is flat or better than before:WithValuesconsoleWithValuesjsonWithDeferredValuesconsoleWithDeferredValuesjsonConsole mode gains most: dropping
DevelopmentEncoder's second encoder removes aClone()per derivation and a core plus aNewTeeslice permakeZap. Commit 1 also stopped the deferred path encoding every entry to JSON for the discard tap before throwing it away (−1 alloc, ~12% off dev-mode deferred writes).Breaking
Removes
logger.WithTap,zaputil.NewDiscardWriteEnabler,zaputil.NewEncoderCore, andzaputil.Encoder[T]/DevelopmentEncoder/ProductionEncoderas distinct types. Minor changeset included. Known downstream caller:cloud-protocol/audit_test.go, a one-line swap — it assertsWithValuesfields on the tap, so it is also the live check that a tee accumulates them.Note
Overlaps #1788, which also touches
makeZap/WithMinLevel.