Skip to content

Add centralized structured logging, remove per-module basicConfig calls#856

Open
shiwani42 wants to merge 1 commit intoOWASP:mainfrom
shiwani42:feat/structured-logging
Open

Add centralized structured logging, remove per-module basicConfig calls#856
shiwani42 wants to merge 1 commit intoOWASP:mainfrom
shiwani42:feat/structured-logging

Conversation

@shiwani42
Copy link
Copy Markdown

Summary

Fixes #855

  • Add application/utils/logging_config.py with a configure_logging() function and a JSONFormatter class
  • LOG_LEVEL env var controls log level (default INFO)
  • LOG_FORMAT=json switches to structured JSON output (one object per line), useful for log aggregation
  • Remove logging.basicConfig() and logger.setLevel(logging.INFO) from all 25 affected modules
  • Call configure_logging() from create_app(), cre_main, and worker so every runtime path is covered

No functional changes to application behaviour. Plain text format remains the default.

Test plan

  • 10 new tests in application/tests/logging_config_test.py covering JSONFormatter output, configure_logging() level and format selection, env var overrides, and idempotent handler setup
  • All 10 pass: python -m pytest application/tests/logging_config_test.py
  • No regressions: python -m pytest application/tests/db_test.py application/tests/web_main_test.py
  • LOG_FORMAT=json produces valid JSON lines at runtime

25 modules each called logging.basicConfig() with no arguments and
independently hardcoded logger.setLevel(logging.INFO). Only the first
basicConfig call has any effect; subsequent ones are no-ops, leaving
log format and level effectively undefined for most of the application.

Add application/utils/logging_config.py with:
- configure_logging(): configures the root logger once at startup
- LOG_LEVEL env var controls log level (default INFO)
- LOG_FORMAT=json enables structured JSON output for log aggregation
- JSONFormatter: emits timestamp, level, logger, message, and optional
  exception fields as a single JSON object per line

Remove logging.basicConfig() and logger.setLevel(logging.INFO) from all
25 affected modules. Call configure_logging() from create_app(),
cre_main, and worker entry points so every runtime path is covered.
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.

Centralize logger configuration and adopt structured logging

1 participant