Skip to content

feat: centralize logging and add --verbose, --debug, --log-file flags#86

Open
rthakkar0555 wants to merge 1 commit into
OWASP:mainfrom
rthakkar0555:fix-issue-52
Open

feat: centralize logging and add --verbose, --debug, --log-file flags#86
rthakkar0555 wants to merge 1 commit into
OWASP:mainfrom
rthakkar0555:fix-issue-52

Conversation

@rthakkar0555
Copy link
Copy Markdown
Contributor

Summary

Implements structured logging across the DockSec codebase, replacing
inconsistent print() and console.print() calls with Python's standard
logging module. Adds --verbose, --debug, and --log-file CLI flags
for configurable verbosity.

Closes #52


Problems Fixed

  • No way to increase verbosity when a scan fails silently
  • Mixed output mechanisms (print, logging, rich.console) coexisting
  • Subprocess stderr from Trivy/Hadolint being swallowed silently
  • No --log-file option for CI/CD artifact collection

Changes Made

docksec/utils.py

  • Added configure_logging(verbose, debug, log_file) function
  • Level mapping: --debug → DEBUG, --verbose → INFO, default → WARNING
  • Optional --log-file adds a FileHandler alongside StreamHandler
  • Existing get_custom_logger() preserved untouched

docksec/cli.py

  • Added -v/--verbose — show detailed scan progress
  • Added --debug — surface raw subprocess output and tool stderr
  • Added --log-file PATH — write structured logs to file
  • configure_logging() called immediately after args are parsed

docksec/docker_scanner.py

  • Replaced ~48 print() and console.print() calls with structured logging
  • Scan progress → logger.info()
  • Raw commands → logger.debug()
  • subprocess stderr on failure → logger.error()
  • subprocess stderr on success → logger.debug()

docksec/score_calculator.py

  • Replaced all print() with logger
  • Score results → logger.info()
  • Score components → logger.debug()

docksec/report_generator.py

  • Replaced all print() with logger
  • Report saved paths → logger.info()
  • Warnings (empty data) → logger.warning()
  • Errors → logger.error()

tests/test_logging.py (new)

  • 6 new tests covering all acceptance criteria:
    • Default level = WARNING
    • --verbose sets level = INFO
    • --debug sets level = DEBUG
    • --debug implies --verbose (DEBUG < INFO)
    • --log-file creates and writes to file
    • Zero print() calls remain in library code (AST scan)

Acceptance Criteria Met

  • --verbose shows tool progress without debug noise
  • --debug surfaces raw subprocess output and LLM timings
  • --log-file PATH writes structured logs to file
  • Default output unchanged (no regressions)
  • Zero print() calls remain in library code
  • Unit tests verify log output at different verbosity levels

Validation

pytest tests/ -v → 37 passed, 0 failures
Screenshot 2026-05-19 014523
Screenshot 2026-05-19 014733

(6 new logging tests + 31 existing tests, 0 regressions)

@codecov-commenter
Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 45.31250% with 105 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (main@7a2a48a). Learn more about missing BASE report.

Files with missing lines Patch % Lines
docksec/docker_scanner.py 2.63% 74 Missing ⚠️
docksec/score_calculator.py 0.00% 14 Missing ⚠️
docksec/report_generator.py 37.50% 10 Missing ⚠️
docksec/cli.py 0.00% 5 Missing ⚠️
tests/test_logging.py 97.33% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main      #86   +/-   ##
=======================================
  Coverage        ?   42.68%           
=======================================
  Files           ?       17           
  Lines           ?     1989           
  Branches        ?        0           
=======================================
  Hits            ?      849           
  Misses          ?     1140           
  Partials        ?        0           
Flag Coverage Δ
unittests 42.68% <45.31%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

[ENHANCEMENT] Add structured logging with configurable verbosity levels (--verbose / --debug)

2 participants