Skip to content
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

Replace --debug and --trace with a fine-grained --log-level flag #3703

Merged
merged 1 commit into from
Dec 2, 2024

Conversation

rgmz
Copy link
Contributor

@rgmz rgmz commented Dec 1, 2024

Description:

This was motivated by the discussion in #3589.

It fixes #3668.

Checklist:

  • Tests passing (make test-community)?
  • Lint passing (make lint this requires golangci-lint)?

@rgmz rgmz requested a review from a team as a code owner December 1, 2024 16:00
if l == -1 {
// Zap uses "5" as the value for fatal.
// We need to pass in "-5" because `SetLevel` passes the negation.
log.SetLevel(-5)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This technically works because there's no logs with this level. Perhaps there's a better way to disable logging.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, this is unfortunate if it's the best we can do. If it is, I think it's worth leaving an apologetic, explanatory comment.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This works, but another idea is to use logr.Discard.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not familiar enough with the logging setup. How would you implement logr.Discard?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this PR provides a cleaner interface to logging and is in a mergable state. I'm unfamiliar with logr.Discard as well. We can always follow-up if it turns out this change needs more.

Copy link
Collaborator

@rosecodym rosecodym left a comment

Choose a reason for hiding this comment

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

This is neat, thanks for doing it! I would like to also get the opinions of @zricethezav and @mcastorina.

Comment on lines +294 to +295
// Zap uses "5" as the value for fatal.
// We need to pass in "-5" because `SetLevel` passes the negation.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I have some dumb questions:

  • What does "uses 5 as the value for fatal" mean here? Are fatal errors are logged at level 5?
  • Why does SetLevel pass the negation? That seems really strange.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What does "uses 5 as the value for fatal" mean here? Are fatal errors are logged at level 5?

Zap's logging levels use 0 for info, with negatives increasing verbosity and 1-5 decreasing verbosity.

TruffleHog only seems to use levels <= 0, so setting the level to 5 effectively prevents anything from being logged.

Why does SetLevel pass the negation? That seems really strange.

My guess is that someone preferred the look of (+)1->5 for verbosity and not (-)1->5. 🤷‍♂️

Copy link
Collaborator

Choose a reason for hiding this comment

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

It originates from the combination of zap as the log implementation for logr.

  • logr defines higher numbers as being more verbose
  • zap defines lower numbers as being more verbose

I left a comment about it in SetLevel:

// Zap's levels get more verbose as the number gets smaller, as explained
// by zapr here: https://github.com/go-logr/zapr#increasing-verbosity
// For example setting the level to -2 below, means log.V(2) will be enabled.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I didn't realize it was two different implementations.

if l == -1 {
// Zap uses "5" as the value for fatal.
// We need to pass in "-5" because `SetLevel` passes the negation.
log.SetLevel(-5)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, this is unfortunate if it's the best we can do. If it is, I think it's worth leaving an apologetic, explanatory comment.

@zricethezav zricethezav merged commit 8f1ba8b into trufflesecurity:main Dec 2, 2024
13 checks passed
@rgmz rgmz deleted the feat/log-level branch December 2, 2024 18:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

How to suppress info log output?
4 participants