mirror of
https://github.com/pewdiepie-archdaemon/odysseus.git
synced 2026-10-09 15:37:51 +00:00
fix(logging): apply log level to uvicorn entrypoints
This commit is contained in:
parent
137a9c1c9f
commit
845d7ea89b
4 changed files with 35 additions and 0 deletions
2
app.py
2
app.py
|
|
@ -87,12 +87,14 @@ from core.log_safety import (
|
|||
CAPABILITY_DIAGNOSTICS_LOGGER,
|
||||
ScopedDiagnosticsFilter,
|
||||
application_log_settings,
|
||||
configure_uvicorn_log_levels,
|
||||
)
|
||||
|
||||
_root_logger = logging.getLogger()
|
||||
_log_level_name = os.getenv("LOG_LEVEL", "INFO").strip().upper()
|
||||
_application_log_level, _capability_debug = application_log_settings(_log_level_name)
|
||||
_root_logger.setLevel(_application_log_level)
|
||||
configure_uvicorn_log_levels(_application_log_level)
|
||||
logging.getLogger(CAPABILITY_DIAGNOSTICS_LOGGER).setLevel(
|
||||
logging.DEBUG if _capability_debug else logging.NOTSET
|
||||
)
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ from urllib.parse import urlparse, urlunparse
|
|||
|
||||
|
||||
CAPABILITY_DIAGNOSTICS_LOGGER = "src.model_capability_readers"
|
||||
UVICORN_LOGGER_NAMES = ("uvicorn", "uvicorn.error", "uvicorn.access")
|
||||
|
||||
_LOG_LEVELS = {
|
||||
"DEBUG": logging.DEBUG,
|
||||
|
|
@ -41,6 +42,17 @@ def application_log_settings(value: object) -> tuple[int, bool]:
|
|||
return max(requested, logging.INFO), requested == logging.DEBUG
|
||||
|
||||
|
||||
def configure_uvicorn_log_levels(application_level: int) -> None:
|
||||
"""Apply the mapped app level to Uvicorn's non-propagating loggers.
|
||||
|
||||
External entrypoints configure these loggers before importing ``app`` and
|
||||
otherwise bypass the root logger's level and scoped diagnostics filter.
|
||||
"""
|
||||
|
||||
for logger_name in UVICORN_LOGGER_NAMES:
|
||||
logging.getLogger(logger_name).setLevel(application_level)
|
||||
|
||||
|
||||
class ScopedDiagnosticsFilter(logging.Filter):
|
||||
"""Allow normal application records plus one explicitly scoped DEBUG log."""
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ from core.log_safety import (
|
|||
CAPABILITY_DIAGNOSTICS_LOGGER,
|
||||
ScopedDiagnosticsFilter,
|
||||
application_log_settings,
|
||||
configure_uvicorn_log_levels,
|
||||
redact_url,
|
||||
)
|
||||
|
||||
|
|
@ -65,6 +66,25 @@ def test_application_log_settings_scope_debug_and_fail_closed(
|
|||
)
|
||||
|
||||
|
||||
def test_configure_uvicorn_log_levels_clamps_non_propagating_loggers():
|
||||
logger_names = ("uvicorn", "uvicorn.error", "uvicorn.access")
|
||||
previous_levels = {
|
||||
name: logging.getLogger(name).level for name in logger_names
|
||||
}
|
||||
try:
|
||||
for name in logger_names:
|
||||
logging.getLogger(name).setLevel(logging.DEBUG)
|
||||
|
||||
configure_uvicorn_log_levels(logging.ERROR)
|
||||
|
||||
assert all(
|
||||
logging.getLogger(name).level == logging.ERROR for name in logger_names
|
||||
)
|
||||
finally:
|
||||
for name, level in previous_levels.items():
|
||||
logging.getLogger(name).setLevel(level)
|
||||
|
||||
|
||||
def _record(name: str, level: int) -> logging.LogRecord:
|
||||
return logging.LogRecord(name, level, __file__, 1, "message", (), None)
|
||||
|
||||
|
|
|
|||
|
|
@ -62,6 +62,7 @@ def test_web_app_logging_uses_existing_log_level_environment_toggle():
|
|||
assert 'os.getenv("LOG_LEVEL", "INFO")' in source
|
||||
assert "application_log_settings(_log_level_name)" in source
|
||||
assert "_root_logger.setLevel(_application_log_level)" in source
|
||||
assert "configure_uvicorn_log_levels(_application_log_level)" in source
|
||||
assert "_console_h.addFilter(_diagnostics_filter)" in source
|
||||
assert "_file_h.addFilter(_diagnostics_filter)" in source
|
||||
assert "log_level=_application_log_level" in source
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue