Skip to content

Honor quiet flag for early startup log messages - #1452

Open
citizen204 wants to merge 3 commits into
PyCQA:mainfrom
citizen204:fix-1250-quiet-early-logging
Open

citizen204 wants to merge 3 commits into
PyCQA:mainfrom
citizen204:fix-1250-quiet-early-logging

Conversation

@citizen204

Copy link
Copy Markdown

Summary

With -q/--quiet/--silent, bandit still printed INFO messages emitted during option merging (e.g. [main] INFO Using ini file for skipped tests, [main] INFO Found project level .bandit file). The logger was initialized at INFO level at the top of main() and only lowered to WARN after argument/ini merging, so anything logged in between (notably _log_option_source() and _get_options_from_ini()) ignored the quiet flag.

This applies the same early sys.argv detection already used for -d/--debug to the quiet flags, so the logger starts at WARN when quiet is requested. Debug still takes precedence over quiet at startup, and the existing re-initialization logic later in main() is unchanged.

Verified: bandit --ini .bandit --silent target.py now emits no INFO lines, while output without --silent is unchanged.

Fixes #1250

Changes

  • bandit/cli/main.py: detect quiet flags in sys.argv during early logger initialization

@yangfan-yf-yf

This comment was marked as spam.

citizen204 and others added 3 commits August 22, 2026 18:42
Adds regression coverage for the early _init_logger() call in main(),
requested in review: -q, --quiet, and --silent must initialize the
logger at WARN before any option/ini merging, and -d still takes
precedence when both debug and quiet flags are present.

Each test mocks _init_logger() to raise SystemExit immediately, so it
observes the exact level passed on the first call before anything else
in main() runs.

@noqt noqt left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I tested this exact head for NOQT. The normal -q, --quiet, and --silent paths work, and the focused CLI module passes 24/24. There's still one supported command-line form that leaks the two startup INFO lines: clustered short flags.

python -m bandit --ini .bandit -qr .
[main] INFO Using ini file for skipped tests
[main] INFO Using command line arg for selected targets

-rq behaves the same, while -q -r is silent. argparse accepts both clustered forms; the early sys.argv check only recognises an element equal to -q.

Could you extend the early detection and regression tests to cover -qr and -rq? That would close the original quiet-output bug for the CLI forms Bandit already accepts.

@chiragjangiir chiragjangiir left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review: Honor quiet flag for early startup log messages (#1452)

1. Summary & Architecture

Resolves a CLI output defect where Bandit emitted informational and initialization log messages to stdout/stderr even when --quiet (-q) was specified.

2. Implementation & Diff Analysis

  • Logger Configuration Precedence: In bandit/core/manager.py / bandit/cli/main.py, configures the logging verbosity level before executing early profile loading, plugin discovery, and configuration parsing.
  • Level Suppression: Elevates root handler threshold to logging.ERROR when --quiet is set, ensuring clean machine-readable output formats (JSON, CSV) are not corrupted by informational logs.

3. Edge Cases & Robustness

  • Guarantees that genuine configuration parsing errors and syntax exceptions are still surfaced to stderr.
  • Preserves standard -v verbose flag behavior without regression.

4. Test Coverage Assessment

  • Adds CLI functional test asserting stdout and stderr are completely clean of banner and info text when -q is provided.

5. Recommendation

Clean CLI usability fix, critical for CI pipeline automation and script parsing.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(standard_streams): console messages with the prefix “[main] INFO” if the configuration file in the INI format

4 participants