-
Notifications
You must be signed in to change notification settings - Fork 1.8k
pkg/log/zap: adding helpers for zap logger configuration #873
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
Conversation
shawn-hurley
left a 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.
I am oddly concerned about the meta flag zap-devel (maybe change this flag name or add a shorthand?) controlling the defaults for the other flags. At the very least we need to expose this behavior to the user via the help text IMO.
Overall this is on the right track and as discussed offline, we should see if the upstream zapr project may want to have their flags accessible in this way.
…Set function with others
|
@shawn-hurley For now, I've updated the The one concern I have about using a shorthand in this case is that we currently have this |
|
We can always add a shorthand, and your right about the prefix. Instead of |
lilic
left a 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.
Few questions.
estroz
left a 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.
Looks good overall but the factory seems unnecessary. Your suggestion is a good alternative.
Moves the zap flagset initialization into the ansible and helm flag packages so that they are reused in all ansible and helm operator scenarios (e.g. `up local`, `run`, and the helm binary) Also reverts parts a previous commit (975eddc) that added flags to the ansible and helm operators imported by other libraries because it caused the entire set of "testing" package flags to be added (from `operator-sdk` dependencies).
lilic
left a 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.
lgtm
pkg/log/zap/flags.go
Outdated
| zapFlagSet = pflag.NewFlagSet("zap", pflag.ExitOnError) | ||
| zapFlagSet.BoolVar(&development, "zap-devel", false, "Enable zap development mode (changes defaults to console encoder, debug log level, and disables sampling)") | ||
| zapFlagSet.Var(&encoderVal, "zap-encoder", "Zap log encoding ('json' or 'console')") | ||
| zapFlagSet.Var(&levelVal, "zap-level", "Zap log level (one of 'debug', 'info', 'warn', 'error', 'dpanic', 'panic', 'fatal')") |
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.
@joelanford I'm wondering what happens if you set --zap-level=warn.
From my recollection of zap levels this will set the minimum level to WarnLevel or +1. Meaning any logs at lower levels like InfoLevel (0) and Debug (-1) won't be shown.
I mentioned how zap levels map to logr/zapr levels in a previous comment.
Given that mapping, setting --zap-level to anything above Info won't be of much use since the highest priority level that you can log with logr is v(0) or Info (0). The rest are just Debug levels v(1)==>Debug (-1) v(2)==>Custom Debug (-2) and so on.
So I would propose this flag should only let you set Info and Debug levels: 0,-1,-2,-3,.... since this logger is always meant to be used in conjunction with zapr via logf.SetLogger(zap.Logger()).
Unless of course I'm completely off about my initial assumption of --zap-level=warn.
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 think you're correct. I'll verify and fix.
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.
Just checked and found a few things:
Error()is part of thelogr.Loggerinterface, so at a minimum we would needdebug,info, anderror.- It's possible to get handles to each of the log levels using the
logr.Loggerinterface :log.V(2).Info("DEBUG Level 2") log.V(1).Info("DEBUG Level 1") log.V(0).Info("INFO Level (with specific verbosity number)") log.Info("INFO Level (without specific verbosity number)") log.V(-1).Info("WARN Level") log.V(-2).Info("", "error", "ERROR Level (with specific verbosity number)") log.Error(errors.New("ERROR Level (without specific verbosity number)"), "") log.V(-3).Info("DPANIC Level") log.V(-4).Info("PANIC Level") log.V(-5).Info("FATAL Level")
- It is not currently possible to set a log level to get
log.V(2)(or higher DEBUG levels) to show up. Onlylog.V(1)will output debug logs. If we allow integers for the--zap-levelflag, we could probably make higher DEBUG levels work.
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.
Yeah I think it's fine if we allow integers for --zap-level.
What should the allowed range be though? I'm a little doubtful of the utility of allowing users to set it to anything above 0(info).
Say --zap-level=1 or warn. In this case all info(0) and debug(-1,-2,..) logs will not be shown.
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 would say that we should allow 0 to infinity (0 = info, 1 = debug, 2=debug2, etc.) for integers. That would mean there would be overlap for info/0 and debug/1 values, but I think that's better than seemingly switching arbitrarily from string to integer levels at 2 and above.
I think the integer should be positive and increasing for higher debug levels to match the integer that's used in the logr interface. So we'd end up allowing the following flag values:
fatalpanicdpanicerrorwarninfo(optionally allow0?)debug(optionally allow1?)23- etc...
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.
Makes sense. Having integers for 0,1,2,3... is fine.
Logr discourages using anything other than info, error and integer debug levels so ideally users just stick to the integers.
But if for some reason users want to set the minimum level to warn that skips all info and debug logs, well I guess we'll allow that.
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.
For the record, I'm not opposed to slimming the accepted values down to the following to be more in line with logr:
errorinfo/0debug/123- etc...
At that point we could probably name the flag --log-level, since it would be applicable to any logr implementation.
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.
@hasbro17 now we're back full circle. Hitting this panic trying to set the log level to higher debug levels: kubernetes-sigs/controller-runtime#167 (comment)
Zap has a well-defined set of levels between debug and fatal, so it makes sense that trying to use a log level outside of that range would fail.
I guess this means there's no need to support integers for the flag since it is zap-specific? Any opinions on using all of them or just the ones relevant to logr? I think I lean toward only the logr ones (debug, info, and error).
I'll find some place to document that log.V(2) (or higher) is not supported by the zap logger.
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.
@joelanford I forgot about that bug. I was sure that zap supports custom debug levels like v(-2) and that bug was actually in zapr. But I'll need to take a closer look.
For now I agree that we can keep this simple and just provide debug, info, and error as configurable levels and be in line with logr conventions. And we can document log.V(2) as not being supported.
hasbro17
left a 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.
LGTM
Description of the change:
Adds a helper to register flags for the zap logger configuration and create a
logr.Loggerbased on those flags.This is based on the idea I suggested in #870. I'm by no means set on this implementation, but wanted to get it out there to help with the discussion.
Motivation for the change:
See #870