Skip to content

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

Merged
merged 6 commits into from
Nov 14, 2018

Conversation

estroz
Copy link
Member

@estroz estroz commented Nov 5, 2018

Description of the change:

  1. Switch the operator-sdk binary's logger from a mix of std log and logrus to only logrus.
  2. Use controller-runtime/pkg/runtime/log.SetLogger() to inject logr.Logger that users can define into their operator.
    • The SDK injects zap by default in this PR.
    • pkg/sdk/*metrics* and pkg/ansible are used by generated operators, which is why these packages don't use logrus.

Motivation for the change: Operators generated by the SDK did not have a uniform logging interface pre-v0.1.0. Now that controller-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. The operator-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

@openshift-ci-robot openshift-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Nov 5, 2018
@estroz estroz added kind/feature Categorizes issue or PR as related to a new feature. refactoring design and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Nov 5, 2018
@openshift-ci-robot openshift-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Nov 5, 2018
@estroz estroz changed the title Logging SDK log injection and uniform logging in operator-sdk binary Nov 5, 2018
@estroz
Copy link
Member Author

estroz commented Nov 6, 2018

Note: I'm removing package-level logging in favor of a more configurable setup.

@estroz estroz changed the title SDK log injection and uniform logging in operator-sdk binary [WIP] SDK log injection and uniform logging in operator-sdk binary Nov 6, 2018
@openshift-ci-robot openshift-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Nov 6, 2018
@openshift-ci-robot openshift-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Nov 6, 2018
@estroz estroz force-pushed the logging branch 4 times, most recently from 35c6fd1 to b28af58 Compare November 7, 2018 19:50
@estroz estroz mentioned this pull request Nov 7, 2018
VDebug LogVerbosity = iota - 1
_
// VWarn == 1. log.V(VWarn).Info(...) == log.Warn(...)
VWarn
Copy link
Contributor

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.

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)

Copy link
Contributor

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.

@shawn-hurley @joelanford

Copy link
Member

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?

Copy link
Member

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.

Copy link
Member Author

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

Copy link
Member

@shawn-hurley shawn-hurley left a 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!

VDebug LogVerbosity = iota - 1
_
// VWarn == 1. log.V(VWarn).Info(...) == log.Warn(...)
VWarn
Copy link
Member

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.

@estroz estroz force-pushed the logging branch 2 times, most recently from ac6ef54 to 3ff89dc Compare November 14, 2018 20:17
Copy link
Contributor

@hasbro17 hasbro17 left a 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.

@estroz estroz merged commit 4bf0c69 into operator-framework:master Nov 14, 2018
@estroz estroz deleted the logging branch November 14, 2018 21:58
@@ -0,0 +1,105 @@
# Logging in operators
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design kind/feature Categorizes issue or PR as related to a new feature. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve error message when no resources have been added to operator Default logger for the SDK
8 participants