Skip to content

Commit

Permalink
fix: AttributeError when using LOGGING_CONFIGURATION_FILE environ…
Browse files Browse the repository at this point in the history
…ment variable (#4693)
  • Loading branch information
khvn26 authored Oct 16, 2024
1 parent 714a68b commit 2aad0a1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
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

0 comments on commit 2aad0a1

Please sign in to comment.