Skip to content

Commit 9bff031

Browse files
committed
logging: always stringify job args, regardless of whether there are any.
1 parent 112d0e0 commit 9bff031

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/dreng/logging.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
class JobArgsFilter(logging.Filter):
1212
def filter(self, record: logging.LogRecord) -> logging.LogRecord:
13-
if (job := getattr(record, "job", {})) and isinstance(job, dict) and (args := job.get("args")):
14-
job["args"] = str(args)[:1000] # Avoid "overflowing" logging infrastructure.
13+
if (job := getattr(record, "job", {})) and isinstance(job, dict) and "args" in job:
14+
job["args"] = str(job["args"])[:1000] # Avoid "overflowing" logging infrastructure.
1515
return record
1616

1717

tests/logging.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@
1111
("extra", "expected_job"),
1212
[
1313
(
14-
{"slack": {"chill": True}, "job": {"some_number": 123, "args": [1, 2, 3]}},
14+
{"job": {"some_number": 123, "args": [1, 2, 3]}},
1515
{"some_number": 123, "args": "[1, 2, 3]"},
1616
),
1717
(
18-
{"slack": {"chill": True}},
18+
{},
1919
{},
2020
),
2121
(
2222
{
23-
"slack": {"chill": True},
2423
"job": {
2524
"created_at": datetime_utc(2025, 12, 25, 13, 37),
2625
"execute_at": datetime_utc(2025, 12, 24, 14, 47),
@@ -30,19 +29,28 @@
3029
),
3130
(
3231
{
33-
"slack": {"chill": True},
3432
"job": {
3533
"created_at": TransactionNow(),
3634
"execute_at": TransactionNow(),
3735
},
3836
},
3937
{"created_at": None, "execute_at": None},
4038
),
39+
(
40+
{"job": {"args": {}}},
41+
{"args": r"{}"},
42+
),
4143
],
4244
)
4345
def test_JobArgsFilter(caplog: pytest.LogCaptureFixture, extra: dict[str, Any], expected_job: dict[str, Any]) -> None:
4446
logger = logging.getLogger("tests")
45-
logger.info("Working hard or hardly working?", extra=extra)
47+
logger.info(
48+
"Working hard or hardly working?",
49+
extra={
50+
**extra,
51+
"slack": {"chill": True}, # Should always be kept as is.
52+
},
53+
)
4654
(record,) = caplog.records
4755
assert hasattr(record, "slack")
4856
assert record.slack["chill"] is True

0 commit comments

Comments
 (0)