Skip to content

Commit cab92c3

Browse files
committed
Avoid TransactionNow instances ending up in logging.
1 parent 263a2a7 commit cab92c3

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/dreng/logging.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import logging
22
from logging import INFO, WARNING
33

4+
from django.contrib.postgres.functions import TransactionNow
5+
46
__all__ = ["INFO", "WARNING", "getLogger"]
57

68

79
class JobArgsFilter(logging.Filter):
810
def filter(self, record: logging.LogRecord) -> logging.LogRecord:
9-
if (job := getattr(record, "job", {})) and isinstance(job, dict) and "args" in job:
10-
job["args"] = str(job["args"])[:1000] # Avoid "overflowing" logging infrastructure.
11+
if (job := getattr(record, "job", {})) and isinstance(job, dict):
12+
if args := job.get("args"):
13+
job["args"] = str(args)[:1000] # Avoid "overflowing" logging infrastructure.
14+
if (created_at := job.get("created_at")) and isinstance(created_at, TransactionNow):
15+
job["created_at"] = None # Avoid 'TransactionNow()' ending up in logging infrastructure.
16+
if (execute_at := job.get("execute_at")) and isinstance(execute_at, TransactionNow):
17+
job["execute_at"] = None # Avoid 'TransactionNow()' ending up in logging infrastructure.
1118
return record
1219

1320

tests/logging.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from typing import Any
22

33
import pytest
4+
from django.contrib.postgres.functions import TransactionNow
45

56
from dreng import logging
7+
from tests.helpers import datetime_utc
68

79

810
@pytest.mark.parametrize(
@@ -16,6 +18,26 @@
1618
{"slack": {"chill": True}},
1719
{},
1820
),
21+
(
22+
{
23+
"slack": {"chill": True},
24+
"job": {
25+
"created_at": datetime_utc(2025, 12, 25, 13, 37),
26+
"execute_at": datetime_utc(2025, 12, 24, 14, 47),
27+
},
28+
},
29+
{"created_at": datetime_utc(2025, 12, 25, 13, 37), "execute_at": datetime_utc(2025, 12, 24, 14, 47)},
30+
),
31+
(
32+
{
33+
"slack": {"chill": True},
34+
"job": {
35+
"created_at": TransactionNow(),
36+
"execute_at": TransactionNow(),
37+
},
38+
},
39+
{"created_at": None, "execute_at": None},
40+
),
1941
],
2042
)
2143
def test_JobArgsFilter(caplog: pytest.LogCaptureFixture, extra: dict[str, Any], expected_job: dict[str, Any]) -> None:

0 commit comments

Comments
 (0)