refactor: migrate zapcore.Level to RegisterIntEnum#117
Closed
leodido wants to merge 2 commits into
Closed
Conversation
zapcore.Level to RegisterIntEnum
e26e099 to
f90cb75
Compare
8a836ad to
90453b6
Compare
Owner
Author
Stack Review: PR #117Verdict: ✅ ApproveClean migration of All findings from the initial review have been addressed in
The decision to keep All tests pass at the updated stack tip ( |
f90cb75 to
de06cbd
Compare
Replace hand-written DefineZapcoreLevelHookFunc and StringToZapcoreLevelHookFunc with a RegisterIntEnum[zapcore.Level] call in enum_builtin.go init(). Removes the zapcore import from internal/hooks/define.go and internal/hooks/decode.go. slog.Level is left as-is because its UnmarshalText supports arithmetic offsets (e.g. DEBUG+1) that a map-based approach cannot express. Co-authored-by: Ona <no-reply@ona.com>
- Add comment noting hardcoded zapcore level map and its trade-off vs delegating to zapcore.ParseLevel (2.5) - Remove stale flagcustom:"true" from zapcoreLevelOptions test struct; zapcore.Level is now in the registry and picked up automatically (2.6) Note: the migration changes the error message format for invalid zapcore levels from 'invalid string for zapcore.Level ...' to 'invalid value "..." for Level' (2.4). Co-authored-by: Ona <no-reply@ona.com>
90453b6 to
e5ea61c
Compare
This was referenced Apr 19, 2026
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.
Description
Replace the hand-written
DefineZapcoreLevelHookFuncandStringToZapcoreLevelHookFuncwith a singleRegisterIntEnum[zapcore.Level](...)call, dogfooding the new enum registration infrastructure from #115/#116.What changed
enum_builtin.go:init()registerszapcore.LevelviaRegisterIntEnumwith all 7 levels (debug through fatal).internal/hooks/define.go:DefineZapcoreLevelHookFunc()function and its"zapcore.Level"registry entry. Removedzapcoreimport.internal/hooks/decode.go:StringToZapcoreLevelHookFunc()function and its"zapcore.Level"registry entry. Removedzapcoreimport.StringToIntEnumHookFuncdirectly. Removed staleflagcustom:"true"fromzapcoreLevelOptionstest struct.Error message change
The error message for invalid zapcore levels changed from:
to:
This is user-visible for anyone matching on error strings.
Why not slog.Level?
slog.Level.UnmarshalTextsupports arithmetic offset syntax (DEBUG+1,INFO-2) that produces arbitrary integer values. A map-basedRegisterIntEnumcannot express this, soslog.Levelkeeps its custom hooks.Trade-off: hardcoded level map
The old hook delegated to
zapcore.ParseLevel(). The new hook uses a hardcoded map of 7 levels. If a future zapcore version adds levels,enum_builtin.gomust be updated. The level set has been stable since zapcore v1; a comment documents this.How to test