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

Fix: still warn on zero verbosity #68

Merged
merged 3 commits into from
Nov 15, 2024
Merged

Conversation

willmurphyscode
Copy link
Contributor

Relates to anchore/grype#2180 and anchore/syft#3081.

This PR makes 2 changes:

  1. If no TTY is present, and neither --verbose nor --quiet is set, log.Warn should be written to stderr.
  2. Allow the environment variables NO_TTY to be set to force disabling the fancy terminal UI even if a TTY is present.

Number 1 will fix the linked bugs in Syft and Grype when this change is pulled into those repositories. Number 2 might be implemented differently or even removed - it really exists to enable testing bugs like number 1.

Previously, there was an issue where if there was no TTY, and verbosity
was zero, the logger would be initialized with io.Discard instead of
stderr. Log to stderr unless "--quiet" is passed.

Signed-off-by: Will Murphy <[email protected]>
@willmurphyscode willmurphyscode self-assigned this Oct 17, 2024
@@ -41,7 +41,7 @@ func DefaultLogger(clioCfg Config, store redact.Store) (logger.Logger, error) {

l, err := logrus.New(
logrus.Config{
EnableConsole: cfg.Verbosity > 0 && !cfg.Quiet,
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the problem that cfg.Verbosity is never set, so defaults to 0 and therefore EnableConsole == false? Is there a way that this configuration could be consolidated to just be based on a verbosity number instead of 3 distinct settings of verbosity, quiet, and level?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is the problem that cfg.Verbosity is never set, so defaults to 0 and therefore EnableConsole == false?

Yes, that's the problem. So if there's no TTY and there's no verbosity, there's nowhere for log.Warn outputs to go, because EnableConsole: false will cause the underlying writer to be io.Discard.

Is there a way that this configuration could be consolidated to just be based on a verbosity number instead of 3 distinct settings of verbosity, quiet, and level?

I don't think it can be simplified beyond this. We want -q but a log file and a verbosity set in the environment to still write to the environment I think? I'm not sure I understand your suggestion.

Copy link
Contributor

@kzantow kzantow Oct 29, 2024

Choose a reason for hiding this comment

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

I'm not suggesting to change any of the exposed options but rather make the options determine one specific verbosity level, and base all logging configuration on that. It looks like we have a function to do that: https://github.com/anchore/clio/blob/main/logging.go#L133 -- this takes all 3 options into consideration and returns a single logging level. We could modify this function to use the result of that function as the logging level, where anywhere we use quiet, instead we use levelFromSelectLevel != logger.Disabled or something like that, and we use that level to set Level: below, etc.

In fact, I'm not entirely sure why the selectLevel() function is modifying the configuration, it should probably not do that and instead just get called in PostLoad only to validate the options, then later in this function to get the effective logging level

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 has a giant TODO in it:

clio/logging.go

Lines 140 to 143 in ceccbdd

// TODO: this is bad: quiet option trumps all other logging options (such as to a file on disk)
// we should be able to quiet the console logging and leave file logging alone...
// ... this will be an enhancement for later
return logger.DisabledLevel, nil

Is that something we should be addressing in this PR? Is there any issue for that?

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 TODO actually seems to be saying we don't want to collapse the amount of options here.

make the options determine one specific verbosity level, and base all logging configuration on that

I don't think we can sensibly do that, because we have 4 independent dimensions of input:

  1. quiet - don't do fancy terminal UI or log to the terminal
  2. -v / -vv raise the level of the logger, regardless of where it prints
  3. Whether stdout/stderr are a TTY - determines whether we can do fancy TUI stuff, but shouldn't determine anything else
  4. logfile - change the logger, whatever its level is, to append to this path instead of stderr, regardless of whether --quiet was passed or a TTY is present.

In other words, something like SYFT_LOG_FILE=/tmp/log.txt syft --quiet -vvv ... should be a valid command, that should append trace logs of /tmp/log.txt and not do any fancy TUI stuff or log to stdout or stderr.

Does that make sense? Maybe we should have a discussion about how these options should interact, then revisit this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here is the longer discussion about logging.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually this is the topic I meant to link to.

@willmurphyscode
Copy link
Contributor Author

I removed needs-discussion because we discussed it on yesterday's live stream

There's also a discourse thread for a discussion of configuring logging and TUI output.

@willmurphyscode willmurphyscode merged commit 6a13fc0 into main Nov 15, 2024
4 checks passed
@willmurphyscode willmurphyscode deleted the fix-warn-on-zero-verbosity branch November 15, 2024 14:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants