Add centralized structured logging, remove per-module basicConfig calls#856
Open
shiwani42 wants to merge 1 commit intoOWASP:mainfrom
Open
Add centralized structured logging, remove per-module basicConfig calls#856shiwani42 wants to merge 1 commit intoOWASP:mainfrom
shiwani42 wants to merge 1 commit intoOWASP:mainfrom
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #855
application/utils/logging_config.pywith aconfigure_logging()function and aJSONFormatterclassLOG_LEVELenv var controls log level (defaultINFO)LOG_FORMAT=jsonswitches to structured JSON output (one object per line), useful for log aggregationlogging.basicConfig()andlogger.setLevel(logging.INFO)from all 25 affected modulesconfigure_logging()fromcreate_app(),cre_main, andworkerso every runtime path is coveredNo functional changes to application behaviour. Plain text format remains the default.
Test plan
application/tests/logging_config_test.pycoveringJSONFormatteroutput,configure_logging()level and format selection, env var overrides, and idempotent handler setuppython -m pytest application/tests/logging_config_test.pypython -m pytest application/tests/db_test.py application/tests/web_main_test.pyLOG_FORMAT=jsonproduces valid JSON lines at runtime