Skip to content

Commit

Permalink
Quick fix for teardown issue with caplog and custom formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Mar 8, 2023
1 parent 8156de8 commit 158f251
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions tests/test_jobregistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ def test_just_log_errors(self, caplog):
)
]

def test_job_id_logging(self, requests_mock, oidc_mock, ejr, caplog):
def test_job_id_logging(self, requests_mock, oidc_mock, ejr, caplog, monkeypatch):
"""Check that job_id logging is passed through as logging extra in appropriate places"""
caplog.set_level(logging.DEBUG)

Expand All @@ -554,7 +554,7 @@ def format(self, record: logging.LogRecord):
job_id = getattr(record, "job_id", None)
return f"{record.name}:{job_id}:{record.message}"

caplog.handler.setFormatter(Formatter())
monkeypatch.setattr(caplog.handler, "formatter", Formatter())

job_id = "j-123"

Expand Down
4 changes: 2 additions & 2 deletions tests/util/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,13 +346,13 @@ def test_just_log_exceptions_invalid_logger(caplog):
assert caplog.record_tuples == [expected]


def test_just_log_exceptions_extra(caplog):
def test_just_log_exceptions_extra(caplog, monkeypatch):
class Formatter:
def format(self, record: logging.LogRecord):
foo = getattr(record, "foo", None)
return f"[Foo:{foo}] {record.levelname} {record.message}"

caplog.handler.setFormatter(Formatter())
monkeypatch.setattr(caplog.handler, "formatter", Formatter())

with just_log_exceptions(extra={"foo": "bar"}):
raise RuntimeError("Nope")
Expand Down

0 comments on commit 158f251

Please sign in to comment.