Skip to content

zapcore: Add PreWriteHook for transforming entries before write#1534

Merged
sywhang merged 2 commits intouber-go:masterfrom
sywhang:prewritehook
Mar 16, 2026
Merged

zapcore: Add PreWriteHook for transforming entries before write#1534
sywhang merged 2 commits intouber-go:masterfrom
sywhang:prewritehook

Conversation

@sywhang
Copy link
Contributor

@sywhang sywhang commented Mar 12, 2026

This adds a new CheckPreWriteHook type and a Before method on CheckedEntry that allows transforming the Entry and Fields before they are written to any registered Cores.

The existing CheckWriteHook (set via After/WithFatalHook) only runs after cores have already written the entry, which means it cannot modify what gets logged. PreWriteHook fills that gap: it runs before any Core.Write call, allowing callers to enrich, redact, or reshape log entries and fields in a composable way.

Multiple pre-write hooks run in the order they were added. Like other CheckedEntry methods, Before is safe to call on nil references.

This adds a new CheckPreWriteHook type and a Before method on
CheckedEntry that allows transforming the Entry and Fields before they
are written to any registered Cores.

The existing CheckWriteHook (set via After/WithFatalHook) only runs
*after* cores have already written the entry, which means it cannot
modify what gets logged. PreWriteHook fills that gap: it runs before
any Core.Write call, allowing callers to enrich, redact, or reshape
log entries and fields in a composable way.

Multiple pre-write hooks run in the order they were added. Like other
CheckedEntry methods, Before is safe to call on nil references.
@codecov
Copy link

codecov bot commented Mar 12, 2026

Codecov Report

❌ Patch coverage is 92.85714% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 98.39%. Comparing base (16fb16b) to head (aef708b).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
zapcore/entry.go 92.85% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1534      +/-   ##
==========================================
- Coverage   98.54%   98.39%   -0.16%     
==========================================
  Files          53       53              
  Lines        3033     3052      +19     
==========================================
+ Hits         2989     3003      +14     
- Misses         35       41       +6     
+ Partials        9        8       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sywhang sywhang merged commit 0ab0d5a into uber-go:master Mar 16, 2026
6 of 8 checks passed
@sywhang sywhang deleted the prewritehook branch March 16, 2026 16:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants