-
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
provide an easy way to derive a logger that logs at a different level #581
Comments
@rogpeppe Would you mind explaining how your code works, because zap is not easy to customize. |
@etsangsplk It works by creating a wrapper type (coreWithLevel) for the zapcore.Core interface that reimplements the Enabled and Check methods to choose a higher logging level. There's an existing option that allows replacing the zapcore.Core associated with a given logger, so we use that and the coreWithLevel type to provide a function that can raise the level associated with a given logger. I'm not sure if that will help much - if in doubt, read the docs and/or browse through the code. FWIW we're using the above technique in the WithLevel function here. |
I wonder if WithOption might be what you want here. https://godoc.org/go.uber.org/zap#Logger.WithOptions |
@NathanZook The code in the issue above does use WithOptions. Have you got a particular way of using WithOptions to raise the logging threshold that's simpler than that? |
More digging. How about https://godoc.org/go.uber.org/zap#example-AtomicLevel ? |
AIUI, AtomicLevel can be used to change the level of a whole tree of loggers, not just a subtree based on the logger that's currently available. Can you show how you might think this might work? |
If at the entry point to your controller, you include a defer that sets the AtomicLevel logger to default, then whatever changes you make while the request is being processed are reset when the request is done. You are then free to set the level as you will for "particular paths". |
What if there are other requests proceeding concurrently which you don't want to affect? Doesn't that approach mean that all logging is globally affected? If so, that's not a viable solution for me. |
I've not explored the options when channels are involved. I see that zapcore.Core.clone is not exported. Nor is ioCore. So, if you built your own core originally, and kept the parameters around... originalCore = zapcore.NewCore(enc, ws, originalLevelEnabler)
logger = zap.New(originalCore, variousOptions...)
// Whatever else is needed to set up the logger--might not be trivial. And you need to
// ensure that the changes don't affect the core.
newCore = zapcore.NewCore(enc, ws, newLevelEnabler)
newLevelLoggerOption = zap.WrapCore(func(zapcore.Core) zapcore.Core {
return newCore
})
newLevelLogger = logger.WithOptions(newLevelLoggerOption) That seems like it would work. Perhaps more obvious or at least clear? |
@NathanZook Some code is missing so I can't be sure, but isn't that similar to the code that I suggested when I raised the issue initially? |
example:
|
@rogpeppe Similar, to be sure. Zap's design is such that if you want to dynamically manage levels, you're going to have to keep ahold of the LevelEnabler yourself, one way or another. |
FYI: Note that with the code from the first post you will not be able to decrease logging level threshold (e.g. from INFO to DEBUG), only increase. To allow level decreasing, you need to eliminate checks on original core: type coreWithLevel struct {
zapcore.Core
level zapcore.Level
}
func (c *coreWithLevel) Enabled(level zapcore.Level) bool {
return c.level.Enabled(level)
}
func (c *coreWithLevel) Check(e zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
if !c.level.Enabled(e.Level) {
return ce
}
return ce.AddCore(e, c.Core)
} |
Note that #581 (comment) does not work with all underlying wrapped cores. For example, this code does not print the log, whatever the level (the
This prints only:
And removing the This means that it's not possible in current implementation to decrease log level of a logger in all circumstances. |
+1 That's exactly right, it's not possible to decrease the log level in all cases, but it is possible to increase the log level, so we added an explicit API to increase the level. It's not safe to call
Since we have an API to increase the level, and there's no decrease the level in a supported way for all use-cases, I'm going to close this issue. |
When logging HTTP requests, we've found it useful to be able to increase the logging threshold for some requests at particular paths (for example, the trivial endpoint that the Nagios check uses) to avoid overwhelming the logs with noise.
It's possible to do this oneself with something like the below code, but this isn't particularly obvious and probably isn't that efficient, so perhaps it might be worth adding a
WithLevel
method to zap.Logger.The text was updated successfully, but these errors were encountered: