Skip to content

Commit

Permalink
Test logger
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn committed Jun 10, 2024
1 parent cf6da21 commit 7c91fb0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
'loggers': {
'django': {
'handlers': ['console'],
'propagate': True,
'level': 'INFO',
'propagate': True,
},
'security': {
'handlers': ['console'],
Expand Down
17 changes: 16 additions & 1 deletion {{cookiecutter.project_name}}/tests/test_server/test_logging.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
import logging
import re
from typing import Final

import pytest

_LOGGING_FORMAT_RE: Final = re.compile(
"timestamp='.+' level='error' event='Test message' logger='django'",
)


@pytest.fixture(name='logger')
def logger_fixture() -> logging.Logger:
"""Returns the current logger instance."""
return logging.getLogger('django')


@pytest.fixture(autouse=True)
def _redact_caplog_handlers(
caplog: pytest.LogCaptureFixture,
logger: logging.Logger,
) -> None:
"""Pytest inserts custom formatter, we need to reset it back."""
caplog.handler.setFormatter(logger.handlers[0].formatter)


def test_logging_format(
caplog: pytest.LogCaptureFixture,
logger: logging.Logger,
Expand All @@ -18,4 +33,4 @@ def test_logging_format(

logger.error(message)

assert caplog.text == 'abc'
assert _LOGGING_FORMAT_RE.match(caplog.text)

0 comments on commit 7c91fb0

Please sign in to comment.