-
Notifications
You must be signed in to change notification settings - Fork 1.8k
SDK log injection and uniform logging in operator-sdk binary #707
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
Note: I'm removing package-level logging in favor of a more configurable setup. |
35c6fd1
to
b28af58
Compare
4fbc7e4
to
4a98d3a
Compare
f513f11
to
8403a6f
Compare
pkg/logutil/log_util.go
Outdated
VDebug LogVerbosity = iota - 1 | ||
_ | ||
// VWarn == 1. log.V(VWarn).Info(...) == log.Warn(...) | ||
VWarn |
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.
Going on a bit of exposition here:
So logr levels have no concept of Warn, Fatal or anything else. It only has Info and Error.
Logr's Info logs decrease in importance as the levels(0,1,2,...) go up e.g 0 is considered standard Info, 1 could be considered Debug(less important), 2 is like more Debug(even less important).
No levels map to Warn or Fatal which are generally more important than Info and Debug.
Zap levels are the inverse. -2(More Debug) -1(Debug), 0 (Info), 1(Warning) ...
https://godoc.org/go.uber.org/zap/zapcore#Level
But since zapr just flips the logr level it will only end up using the following zap levels: 0(Info), -1(Debug), -2(More Debug) ...
https://github.com/go-logr/zapr/blob/master/zapr.go#L133-L134
https://github.com/go-logr/zapr/blob/master/zapr.go#L34-L37
So zapr's mapping is correct.
In practice because the runtime's helper uses either Development or Production configs for zapr, we can only see 2 (zap)levels of logs.
- Production => zap.Info(0) and higher
- so only logr.V(0) => zap.V(0). Remember logr.V(-1) is illegal.
- Development => zap.Debug(-1) and higher
- so that gives us logr.V(1) => zap.V(-1) and logr.V(0) => zap.V(0) and
https://github.com/kubernetes-sigs/controller-runtime/blob/master/pkg/runtime/log/log.go#L39-L48
- so that gives us logr.V(1) => zap.V(-1) and logr.V(0) => zap.V(0) and
Since logr.V(2) or zap.V(-2) is a custom Debug level those logs will not show up. So logr.V(2) is pointless if we're using the runtime's ZapLogger(development bool)
method.
That's why the runtime only has 2 levels of logs statements right now.
Info or logr.V(0) and Debug logr.V(1)
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.
Unless I'm wrong, currently:
VDebug=-1
and VWarn=0
https://play.golang.org/p/JJa3L4rznIr
Making logr.V(VDebug)
illegal. And Warn would just be Info.
I would prefer not to use these consts and just use logr.V(0)
( which is justlogr.Info()
) and logr.V(1)
.
And if we must have consts then they should only be VInfo=0
and VDebug=1
.
Warnings or VWarn
is going against the logr since there is no level for warnings and V(1)
is not a warning.
Even VDebug=1
is just our alias for a less important log.
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.
And if we must have consts then they should only be VInfo=0 and VDebug=1.
Warnings or VWarn is going against the logr since there is no level for warnings and V(1) is not a warning.
We agreed on warning to be 0 in the meeting as summed up
#503 (comment)
But yes, reading the logr docs again and:
I restrict the API to just 2 types of logs: info and error.
So should we then just go with info and error only, as it also has no concept of debug either and not assign "names" to numbers as suggested by logr docs?
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 prefer not to use these consts and just use logr.V(0)( which is justlogr.Info()) and logr.V(1).
👍
I think it is just easier to deal with IMO.
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'll remove constants and change all Warn
to Info
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! Thank you for doing the ansible stuff and cleaning it up some too!
pkg/logutil/log_util.go
Outdated
VDebug LogVerbosity = iota - 1 | ||
_ | ||
// VWarn == 1. log.V(VWarn).Info(...) == log.Warn(...) | ||
VWarn |
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 prefer not to use these consts and just use logr.V(0)( which is justlogr.Info()) and logr.V(1).
👍
I think it is just easier to deal with IMO.
ac6ef54
to
3ff89dc
Compare
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
Thanks for keeping this on track.
We might want to revisit the choice of the zapr logger implementation later but this should be good enough for now.
pkg/*,example/,test/: use logutil in all code used in generated operators
@@ -0,0 +1,105 @@ | |||
# Logging in operators |
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.
@estroz Forgot one last nit. This should be in doc/user/logging.md
doc/dev
is for developers of the SDK.
Description of the change:
operator-sdk
binary's logger from a mix of stdlog
andlogrus
to onlylogrus
.controller-runtime/pkg/runtime/log.SetLogger()
to injectlogr.Logger
that users can define into their operator.zap
by default in this PR.pkg/sdk/*metrics*
andpkg/ansible
are used by generated operators, which is why these packages don't uselogrus
.Motivation for the change: Operators generated by the SDK did not have a uniform logging interface pre-
v0.1.0
. Now thatcontroller-runtime
is the main source of logs in a generated operator, the decision was made to expose a log injection mechanism so users can choose what logger to use and operators emit uniform, structured logs. See #503 for our detailed discussion. Theoperator-sdk
binary similarly did not have uniform logging.logrus
is a reasonable choice for the binary's logger because warnings are used frequently.Closes #503, closes #732