-
Couldn't load subscription status.
- Fork 31
Allow custom implementation of toZapLevel function
#86
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
base: master
Are you sure you want to change the base?
Conversation
| expectedLogs: []string{ | ||
| `{"level":"info","msg":"test 0","v":0}`, | ||
| `{"level":"debug","msg":"test 1","v":1}`, | ||
| // Logs at verbosity 2 and above are dropped |
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.
This test case illustrates the main problem I'm running into, which a custom toZapLevel solves in test cases below.
703e329 to
344e7ae
Compare
344e7ae to
43dfe86
Compare
Which isn't a problem, is it?
This is a valid problem.
I need to look at your implementation, but I see one issue with this: doesn't this customization need to know who is calling it? We cannot simply map |
| } | ||
|
|
||
| // ToZapLevel overrides the default function mapping logr's numeric level | ||
| // to corresponding zap's level. |
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 you add an example of some real-world usage of this new feature?
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 have described the real world example in #84 (comment)
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.
Also see even more detailed example below: #86 (comment)
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.
The key point that needs to be called out is that there will be several different zapr instances with the same zap backend in the same program: one for each package or set of packages with its own conventions regarding the logr level. If this is possible, then this option makes sense.
Where it breaks down is when different packages use the same logr instance (for example, by getting it from the context) and then use inconsistent log levels. That is what I was wondering about in #86 (comment).
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.
Where it breaks down is when different packages use the same logr instance (for example, by getting it from the context) and then use inconsistent log levels.
Agreed, this can only be fixed by addressing the inconsistency between the original libraries.
Even then, I believe the customisable toZapLevel function is a useful improvement:
- The default behaviour is preserved as is, no hidden or breaking changes
- It allows to fix the mapping for
klog(I believe the current default implementation is still wrong to considerV(0)the only equivalent ofInfoandV(1)to be the only equivalent ofDebugandV(2)and above not even captured by standard zap level at all), i.e. allows
klogToZapLevel := func(lvl int) zapcore.Level {
if lvl >= 4 {
return zapcore.DebugLevel
}
if lvl > 0 {
return zapcore.InfoLevel
}
return zapcore.WarnLevel
}
to be used globally instead of the default if preferred.
I also think it's better to recommend defining custom toZapLevel (i.e. make zapr work for the standard zap config) over zap.NewAtomicLevelAt(zapcore.Level(-2)) suggested in https://github.com/go-logr/zapr?tab=readme-ov-file#increasing-verbosity (i.e. having to change the zap config itself), but it is up to you.
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 believe the customisable toZapLevel function is a useful improvement
Yes, let's merge this. We just need to be sure that developers understand when and how to use it.
I also think it's better to recommend defining custom toZapLevel (i.e. make zapr work for the standard zap config) over zap.NewAtomicLevelAt(zapcore.Level(-2)) suggested in https://github.com/go-logr/zapr?
That depends a bit on how the zap backend gets configured. In component-base/logs we map the program's -v parameter to the zapcore.Level and the current mapping works as intended.
Can you update the documentation as part of your PR?
No, this is not an issue. Then I configure forwarding And then I configure forwarding OpenTelemetry logs to zap: You can find implementations of |
|
@pohly looks like this PR requires some maintainer's approval to get the builds running? |
|
Looks like GitHub actions are no longer working. I won't be able to get to that before sometime next week. |
The standard
toZapLevelimplementation has a few issues:V(2)and above are mapped to negative (non-existent)zapcore.LevelbelowDebugzapis configured atzapcore.DebugLevel, all log messages fromV(2)and above will be droppedThe recommended workaround is to set
zapcore.Level(-4)to see log messages up toV(4)verbosity: #84 (comment)However this also means that all messages logged as
.Debug(...)directly to zap logger will be preserved as well.In addition, there is no consistency between libraries on which verbosity levels map to
InfoorDebug(e.g. client-go and OpenTelemetry), as mentioned in the issue above.Adding support for custom
toZapLevelimplementation would allow to:logruse which we redirect logs from tozapzap