|
| 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