Skip to content

Conversation

@joelanford
Copy link
Member

@joelanford joelanford commented Dec 20, 2018

Description of the change:
Adds a helper to register flags for the zap logger configuration and create a logr.Logger based 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

@openshift-ci-robot openshift-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Dec 20, 2018
@joelanford joelanford changed the title [WIP] pkg/log/zap: adding helpers for zap logger configuration pkg/log/zap: adding helpers for zap logger configuration Jan 2, 2019
@openshift-ci-robot openshift-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jan 2, 2019
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.

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.

@joelanford
Copy link
Member Author

@shawn-hurley For now, I've updated the --zap-devel help text to make it clear that it changes defaults of other flags. What did you have in mind for a different name and/or adding a shorthand?

The one concern I have about using a shorthand in this case is that we currently have this zap- prefix. So any shorthand we use kind of promotes the flag. Maybe that's just me being overly cautious though.

@shawn-hurley
Copy link
Member

We can always add a shorthand, and your right about the prefix. Instead of zap-devel I might prefer zap-development-config or something more verbose but the help text is clear so if others are good I am fine.

Copy link
Member

@lilic lilic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few questions.

Copy link
Member

@estroz estroz left a 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).
Copy link
Member

@lilic lilic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@openshift-ci-robot openshift-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jan 20, 2019
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')")
Copy link
Contributor

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.

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 think you're correct. I'll verify and fix.

Copy link
Member Author

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:

  1. Error() is part of the logr.Logger interface, so at a minimum we would need debug, info, and error.
  2. It's possible to get handles to each of the log levels using the logr.Logger interface :
    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")
  3. It is not currently possible to set a log level to get log.V(2) (or higher DEBUG levels) to show up. Only log.V(1) will output debug logs. If we allow integers for the --zap-level flag, we could probably make higher DEBUG levels work.

Copy link
Contributor

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.

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 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:

  • fatal
  • panic
  • dpanic
  • error
  • warn
  • info (optionally allow 0?)
  • debug (optionally allow 1?)
  • 2
  • 3
  • etc...

Copy link
Contributor

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.

Copy link
Member Author

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:

  • error
  • info/0
  • debug/1
  • 2
  • 3
  • etc...

At that point we could probably name the flag --log-level, since it would be applicable to any logr implementation.

Copy link
Member Author

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.

Copy link
Contributor

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.

@openshift-ci-robot openshift-ci-robot added needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. and removed needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Jan 21, 2019
@openshift-ci-robot openshift-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jan 28, 2019
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants