-
Notifications
You must be signed in to change notification settings - Fork 1.8k
log: allow different log levels #1210
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
|
I updated proxy.go to have 6 levels of log to test this PR out. Here is the output: |
|
Using log level 3 notice that 4, 5, & 6 are no longer visible. |
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.
LGTM
pkg/log/zap/logger.go
Outdated
| // cause index out of bounds errors in the sampling code. | ||
| if levelVal.level < -1 { | ||
| sampleVal.set = false | ||
| c.sample = false |
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.
Do we think it's okay to silently disable sampling when --zap-sample=true?
Instead, we could log.Fatal() or change the API to allow us to return an error. Thoughts?
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.
Yes, I do.
We should document that this is what we do, but TBH how many users are purposefully turning on sampling (which is for log performance in production) AND setting the log level to trace level logs. I would assume this is a mistake and we should probably just make the right decision.
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, this is more of a nit, but I'm a little nervous about this check happening in the if levelVal.set { ... } block. I know we have unit tests now, but if we ever change our default log level to debug, this check would get skipped.
It would probably be better to use levelVal.level to set the default in line 69 and then do another check in the if sampleVal.set { ... } block to make sure we [EDIT: don't] turn sampling back on when debug logging is enabled.
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.
@jmrodri I think we should use c.level to determine this on line 89.
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 moved the if block to just before we return the config.
func getConfig() config {
var c config
// Set the defaults depending on the log mode (development vs. production)
if development {
c.encoder = consoleEncoder()
c.level = zap.NewAtomicLevelAt(zap.DebugLevel)
c.opts = append(c.opts, zap.Development(), zap.AddStacktrace(zap.ErrorLevel))
c.sample = false
} else {
c.encoder = jsonEncoder()
c.level = zap.NewAtomicLevelAt(zap.InfoLevel)
c.opts = append(c.opts, zap.AddStacktrace(zap.WarnLevel))
c.sample = true
}
// Override the defaults if the flags were set explicitly on the command line
if encoderVal.set {
c.encoder = encoderVal.encoder
}
if levelVal.set {
c.level = zap.NewAtomicLevelAt(levelVal.level)
}
if sampleVal.set {
c.sample = sampleVal.sample
}
// Disable sampling when we are in debug mode. Otherwise, this will
// cause index out of bounds errors in the sampling code.
if c.level.Level() < -1 {
c.sample = false
}
return c
} |
I now create a logger in the test to ensure it doesn't cause a panic. With the unaltered code: ...
// Disable sampling when we are in debug mode. Otherwise, this will
// cause index out of bounds errors in the sampling code.
if c.level.Level() < -1 {
c.sample = false
}
return c
} When I run the test, the log works just fine. Now the part I really liked which is why @joelanford suggested it, if I comment out the code that disables sampling, we detect the panic. ...
// Disable sampling when we are in debug mode. Otherwise, this will
// cause index out of bounds errors in the sampling code.
// if c.level.Level() < -1 {
// c.sample = false
// }
return c
} Using the above, we can see the test will panic indicating that clearly we broke something :) Thanks for the suggestion @joelanford |
9056828 to
e55dad6
Compare
joelanford
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 👍
With this feature we can use --zap-level 10 and this will allow
log.V(10).Info("hello world") to be seen in the logs. Prior to this
change debug (log.V(1)) was the lowest value attainable.
We also disable sampling when using an integer less than -1
(i.e. beyond debug). The zap sampling code assumes the lowest number for
sampling is DebugLevel (-1) which isn't the case in this scenario.
Disabling sampling makes sense at this point because sampling is used
for better performing logs which isn't required for trace level type
logs.
We also handle negative values being passed in.
* add logger test * do not allow negative log levels at the CLI * disable sampling later in the call based on review * add note about disabling sampling to help * update sampling doc, fix log level help
With this feature we can use --zap-level 10 and this will allow
log.V(10).Info("hello world") to be seen in the logs. Prior to this
change debug (log.V(1)) was the lowest value attainable.
We also disable sampling when using an integer less than -1
(i.e. beyond debug). The zap sampling code assumes the lowest number for
sampling is DebugLevel (-1) which isn't the case in this scenario.
Disabling sampling makes sense at this point because sampling is used
for better performing logs which isn't required for trace level type
logs.
We also handle negative values being passed in.