Repository navigation
Honor quiet flag for early startup log messages - #1452
citizen204 wants to merge 3 commits into
Conversation
This comment was marked as spam.
This comment was marked as spam.
for more information, see https://pre-commit.ci
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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.ERRORwhen--quietis 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
-vverbose 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
-qis provided.
5. Recommendation
Clean CLI usability fix, critical for CI pipeline automation and script parsing.
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 ofmain()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.argvdetection already used for-d/--debugto 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 inmain()is unchanged.Verified:
bandit --ini .bandit --silent target.pynow emits no INFO lines, while output without--silentis unchanged.Fixes #1250
Changes
bandit/cli/main.py: detect quiet flags insys.argvduring early logger initialization