Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: AttributeError when using LOGGING_CONFIGURATION_FILE environment variable #4693

Merged
merged 3 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion api/tests/unit/util/test_logging.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import json
import logging
import logging.config
import os

import pytest
from gunicorn.config import Config
from pytest_django.fixtures import SettingsWrapper

from util.logging import JsonFormatter

Expand All @@ -25,7 +28,7 @@ def test_json_formatter__outputs_expected(
expected_tb_string = (
"Traceback (most recent call last):\n"
f' File "{expected_module_path}",'
" line 34, in _log_traceback\n"
" line 37, in _log_traceback\n"
" raise Exception()\nException"
)

Expand Down Expand Up @@ -59,3 +62,16 @@ def _log_traceback() -> None:
"exc_info": expected_tb_string,
},
]


def test_gunicorn_json_capable_logger__non_existent_setting__not_raises(
settings: SettingsWrapper,
) -> None:
# Given
del settings.LOG_FORMAT
config = Config()

# When & Then
from util.logging import GunicornJsonCapableLogger

GunicornJsonCapableLogger(config)
2 changes: 1 addition & 1 deletion api/util/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def get_json_record(self, record: logging.LogRecord) -> dict[str, Any]:
class GunicornJsonCapableLogger(GunicornLogger):
def setup(self, cfg: Config) -> None:
super().setup(cfg)
if settings.LOG_FORMAT == "json":
if getattr(settings, "LOG_FORMAT", None) == "json":
self._set_handler(
self.error_log,
cfg.errorlog,
Expand Down
Loading