-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
zapcore: Add LevelOf(LevelEnabler), UnknownLevel #1147
Conversation
Add a new function LevelOf that reports the minimum enabled log level for a LevelEnabler. This works by looping through all known Zap levels in-order, returning the newly introduced UnknownLevel if none of the known levels are enabled. A LevelEnabler or Core implementation may implement the `Level() Level` method to override and optimize this behavior. AtomicLevel already implemented this method. This change adds the method to all Core implementations shipped with Zap. Note: UnknownLevel is set at FatalLevel+1 to account for the possibility that users of Zap are using DebugLevel-1 as their own custom log level--even though this isn't supported, it's preferable not to break these users. Users are less likely to use FatalLevel+1 since Fatal means "exit the application." Resolves #1144 Supersedes #1143, which was not backwards compatible
Codecov Report
@@ Coverage Diff @@
## master #1147 +/- ##
==========================================
+ Coverage 98.32% 98.33% +0.01%
==========================================
Files 49 49
Lines 2146 2160 +14
==========================================
+ Hits 2110 2124 +14
Misses 28 28
Partials 8 8
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
zapcore/level.go
Outdated
// UnknownLevel is an invalid value for Level. | ||
// | ||
// Core implementations may panic if they see messages of this level. | ||
UnknownLevel = _maxLevel + 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we reorganize these so that we have an invalid zero value? e.g.
const (
InvalidLevel = iota
DebugLevel
// ...
FatalLevel
_minLevel = DebugLevel
_maxLevel = FatalLevel
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't this be a breaking change because the integer value of DebugLevel (and everything else) would change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I lean towards not changing values of published constants, even if the line around whether it's a breaking change is a bit blurry. Such a change will break someone, and we don't get much in exchange.
zapcore/level.go
Outdated
// UnknownLevel is an invalid value for Level. | ||
// | ||
// Core implementations may panic if they see messages of this level. | ||
UnknownLevel = _maxLevel + 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: s/Unknown/Invalid/
Add a new function LevelOf that reports the minimum enabled log level
for a LevelEnabler.
This works by looping through all known Zap levels in-order,
returning the newly introduced UnknownLevel if none of the known levels
are enabled.
A LevelEnabler or Core implementation may implement the
Level() Level
method to override and optimize this behavior.
AtomicLevel already implemented this method.
This change adds the method to all Core implementations shipped with
Zap.
Note:
UnknownLevel is set at FatalLevel+1 to account for the possibility that
users of Zap are using DebugLevel-1 as their own custom log level--even
though this isn't supported, it's preferable not to break these users.
Users are less likely to use FatalLevel+1 since Fatal means "exit the
application."
Refs #1144
Supersedes #1143, which was not backwards compatible
Refs GO-1586