Skip to content

logger: replace WithTap with WithTee - #1790

Open
paulwe wants to merge 4 commits into
mainfrom
logger-with-tee
Open

logger: replace WithTap with WithTee#1790
paulwe wants to merge 4 commits into
mainfrom
logger-with-tee

Conversation

@paulwe

@paulwe paulwe commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

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, 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:

  1. replay deferred writes through CheckDeferrer.write/flush called the wrapped core's Write directly. 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.
  2. replace WithTap with WithTee — the API change, plus allocation benchmarks.
  3. replace encoderCore with zapcore.NewCore — with one destination per core, encoderCore is ioCore plus the write-time level check that commit 1 made redundant.
  4. 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 OrLevelEnabler and the levelEnablers cache go away entirely.

Allocations

WithValues/WithDeferredValues run per participant and track, so logger/logger_bench_test.go covers objects allocated on both, with and without a tee. Every no-tee path is flat or better than before:

before after
WithValues console 18 10
WithValues json 11 10
WithDeferredValues console 14 9
WithDeferredValues json 10 9
deferred write, console 10 9
deferred flush, console 30 24
plain writes 8 / 4 / 1 unchanged

Console mode gains most: dropping DevelopmentEncoder's second encoder removes a Clone() per derivation and a core plus a NewTee slice per makeZap. 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, and zaputil.Encoder[T]/DevelopmentEncoder/ProductionEncoder as distinct types. Minor changeset included. Known downstream caller: cloud-protocol/audit_test.go, a one-line swap — it asserts WithValues fields on the tap, so it is also the live check that a tee accumulates them.

Note

Overlaps #1788, which also touches makeZap/WithMinLevel.

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-bot

changeset-bot Bot commented Sep 9, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 613798c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
github.com/livekit/protocol Minor
@livekit/protocol Minor

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.

@cnderrauber cnderrauber left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Will wait for this PR to land

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants