-
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
Getting level from zap logger #1144
Comments
Hey, @krupyansky, Zap does not currently have a way of accessing the log level post-construction. Workaround: Construct your logger by supplying a zap.AtomicLevel. If you're using zap.NewProduction or zap.NewDevelopment, you can do this: atomLevel := zap.NewAtomicLevelAt(zap.InfoLevel)
encoderCfg := zap.NewProductionEncoderConfig()
logger := zap.New(zapcore.NewCore(
zapcore.NewJSONEncoder(encoderCfg),
zapcore.Lock(os.Stdout),
atomLevel,
)) Following that, your atomLevel will always report the current log level.
If you're using zap.Config to load your configuration, you can already access the AtomicLevel it uses for the logger at Config.Level. var cfg zap.Config
// ...
logger, err := cfg.Build()
// ...
atomLevel := cfg.Level
fmt.Println(atomLevel.Level())
// atomLevel will report the logger's current level. Solution To actually solve this problem in Zap, we need to expose a means of reading the current log level. What if the package zapcore
// LevelOf reports the minimum enabled log level in the given Core.
// If the Core implements a "Level() Level" method, LevelOf delegates to it.
func LevelOf(core Core) Level {
leveler, ok := core.(interface{ Level() Level })
if ok { return leveler.Level() }
// ...
} If the Core does not implement the method, LevelOf can make a reasonable guess of the Level by looping through all levels in-order and calling We can also add a What do you think, @mway @sywhang @prashantv? |
That seems reasonable to me. Looping over |
Now, if I want to get level from zap, I need use this code:
This construction
not help me. Inheritance is not work for the case. |
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
@krupyansky
This way, when possible, we'll use the Level() method, but otherwise we'll fall back to trying every level. I've created #1147 implementing this. |
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
v1.23.0 contains this feature - closing this as done! |
How can I get log level from zap logger?
For logrus, this problem is solved in the following way:
https://pkg.go.dev/github.com/sirupsen/logrus#Logger.GetLevel
https://pkg.go.dev/github.com/sirupsen/logrus#Entry
I need a similar feature from zap logger.
The text was updated successfully, but these errors were encountered: