Skip to content

Commit ba159ff

Browse files
authored
Merge pull request #8681 from fstagni/fix_concatenate_v90
[9.0] fix: concatenate list to list
2 parents 3461ed0 + 789bf72 commit ba159ff

2 files changed

Lines changed: 85 additions & 2 deletions

File tree

src/DIRAC/FrameworkSystem/private/standardLogging/Formatter/MicrosecondJsonFormatter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ def __init__(self, *args, **kwargs):
1515
self.converter = time.gmtime
1616

1717
if "reserved_attrs" not in kwargs:
18-
kwargs["reserved_attrs"] = RESERVED_ATTRS + (
18+
kwargs["reserved_attrs"] = list(RESERVED_ATTRS) + [
1919
"spacer",
2020
"headerIsShown",
2121
"timeStampIsShown",
2222
"contextIsShown",
2323
"threadIDIsShown",
2424
"color",
25-
)
25+
]
2626
super().__init__(*args, **kwargs)
2727

2828
def formatTime(self, record, datefmt=None):
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""
2+
Test MicrosecondJsonFormatter
3+
"""
4+
5+
import logging
6+
7+
from DIRAC.FrameworkSystem.private.standardLogging.Formatter.MicrosecondJsonFormatter import MicrosecondJsonFormatter
8+
9+
10+
DIRAC_CUSTOM_ATTRS = [
11+
"spacer",
12+
"headerIsShown",
13+
"timeStampIsShown",
14+
"contextIsShown",
15+
"threadIDIsShown",
16+
"color",
17+
]
18+
19+
20+
def test_init_with_tuple_reserved_attrs():
21+
"""Test that MicrosecondJsonFormatter works when RESERVED_ATTRS is a tuple (python-json-logger v2.x)"""
22+
from pythonjsonlogger import jsonlogger
23+
24+
original = jsonlogger.RESERVED_ATTRS
25+
try:
26+
jsonlogger.RESERVED_ATTRS = ("args", "asctime", "created")
27+
formatter = MicrosecondJsonFormatter()
28+
assert isinstance(formatter.reserved_attrs, set)
29+
for attr in DIRAC_CUSTOM_ATTRS:
30+
assert attr in formatter.reserved_attrs
31+
finally:
32+
jsonlogger.RESERVED_ATTRS = original
33+
34+
35+
def test_init_with_list_reserved_attrs():
36+
"""Test that MicrosecondJsonFormatter works when RESERVED_ATTRS is a list (python-json-logger v3.x+)"""
37+
from pythonjsonlogger import jsonlogger
38+
39+
original = jsonlogger.RESERVED_ATTRS
40+
try:
41+
jsonlogger.RESERVED_ATTRS = ["args", "asctime", "created"]
42+
formatter = MicrosecondJsonFormatter()
43+
assert isinstance(formatter.reserved_attrs, set)
44+
for attr in DIRAC_CUSTOM_ATTRS:
45+
assert attr in formatter.reserved_attrs
46+
finally:
47+
jsonlogger.RESERVED_ATTRS = original
48+
49+
50+
def test_formatTime_microsecond_precision():
51+
"""Test that formatTime produces microsecond precision timestamps"""
52+
formatter = MicrosecondJsonFormatter()
53+
record = logging.LogRecord(
54+
name="test",
55+
level=logging.INFO,
56+
pathname="test.py",
57+
lineno=1,
58+
msg="test message",
59+
args=(),
60+
exc_info=None,
61+
)
62+
record.created = 1700000000.123456
63+
64+
result = formatter.formatTime(record)
65+
assert result.endswith(",123456Z")
66+
67+
68+
def test_formatTime_custom_datefmt():
69+
"""Test that formatTime respects custom datefmt"""
70+
formatter = MicrosecondJsonFormatter()
71+
record = logging.LogRecord(
72+
name="test",
73+
level=logging.INFO,
74+
pathname="test.py",
75+
lineno=1,
76+
msg="test message",
77+
args=(),
78+
exc_info=None,
79+
)
80+
record.created = 1700000000.0
81+
82+
result = formatter.formatTime(record, datefmt="%Y/%m/%d")
83+
assert "/" in result

0 commit comments

Comments
 (0)