|
31 | 31 | from pythonjsonlogger.core import RESERVED_ATTRS, BaseJsonFormatter, merge_record_extra |
32 | 32 | from pythonjsonlogger.json import JsonFormatter |
33 | 33 |
|
| 34 | +if pythonjsonlogger.MSGSPEC_AVAILABLE: |
| 35 | + from pythonjsonlogger.msgspec import MsgspecFormatter |
| 36 | + |
34 | 37 | if pythonjsonlogger.ORJSON_AVAILABLE: |
35 | 38 | from pythonjsonlogger.orjson import OrjsonFormatter |
36 | 39 |
|
37 | | -if pythonjsonlogger.MSGSPEC_AVAILABLE: |
38 | | - from pythonjsonlogger.msgspec import MsgspecFormatter |
| 40 | +if pythonjsonlogger.SIMPLEJSON_AVAILABLE: |
| 41 | + from pythonjsonlogger.simplejson import SimpleJsonFormatter |
| 42 | + |
| 43 | +if pythonjsonlogger.ULTRAJSON_AVAILABLE: |
| 44 | + from pythonjsonlogger.ultrajson import UltraJsonFormatter |
39 | 45 |
|
40 | 46 | ### SETUP |
41 | 47 | ### ============================================================================ |
42 | 48 | ALL_FORMATTERS: list[type[BaseJsonFormatter]] = [JsonFormatter] |
43 | | -if pythonjsonlogger.ORJSON_AVAILABLE: |
44 | | - ALL_FORMATTERS.append(OrjsonFormatter) |
| 49 | + |
45 | 50 | if pythonjsonlogger.MSGSPEC_AVAILABLE: |
46 | 51 | ALL_FORMATTERS.append(MsgspecFormatter) |
47 | 52 |
|
| 53 | +if pythonjsonlogger.ORJSON_AVAILABLE: |
| 54 | + ALL_FORMATTERS.append(OrjsonFormatter) |
| 55 | + |
| 56 | +if pythonjsonlogger.SIMPLEJSON_AVAILABLE: |
| 57 | + ALL_FORMATTERS.append(SimpleJsonFormatter) |
| 58 | + |
| 59 | +if pythonjsonlogger.ULTRAJSON_AVAILABLE: |
| 60 | + ALL_FORMATTERS.append(UltraJsonFormatter) |
| 61 | + |
48 | 62 | _LOGGER_COUNT = 0 |
49 | 63 |
|
50 | 64 |
|
@@ -543,7 +557,15 @@ def json_default(obj: Any) -> Any: |
543 | 557 | env.logger.info("Hello") |
544 | 558 | log_json = env.load_json() |
545 | 559 |
|
546 | | - assert log_json["timestamp"] == "2017-07-14T02:40:00+00:00" |
| 560 | + expected = "2017-07-14T02:40:00+00:00" |
| 561 | + |
| 562 | + if (pythonjsonlogger.SIMPLEJSON_AVAILABLE and class_ is SimpleJsonFormatter) or ( |
| 563 | + pythonjsonlogger.ULTRAJSON_AVAILABLE and class_ is UltraJsonFormatter |
| 564 | + ): |
| 565 | + # simplejson and ujson do not use sep=T |
| 566 | + expected = expected.replace("T", " ") |
| 567 | + |
| 568 | + assert log_json["timestamp"] == expected |
547 | 569 | return |
548 | 570 |
|
549 | 571 |
|
@@ -616,6 +638,29 @@ def test_common_types_encoded( |
616 | 638 | ): |
617 | 639 | pytest.xfail() |
618 | 640 |
|
| 641 | + if (pythonjsonlogger.SIMPLEJSON_AVAILABLE and class_ is SimpleJsonFormatter) or ( |
| 642 | + pythonjsonlogger.ULTRAJSON_AVAILABLE and class_ is UltraJsonFormatter |
| 643 | + ): |
| 644 | + if obj == b"fancy-bytes-\xf0\xf1": |
| 645 | + # simplejson attempts to encode using `utf-8` and thus does not support arbitrary bytes |
| 646 | + # ultrajson prevents bytes or errors when receiving non `utf-8` bytes |
| 647 | + pytest.xfail() |
| 648 | + |
| 649 | + ## Overrides |
| 650 | + if (pythonjsonlogger.SIMPLEJSON_AVAILABLE and class_ is SimpleJsonFormatter) or ( |
| 651 | + pythonjsonlogger.ULTRAJSON_AVAILABLE and class_ is UltraJsonFormatter |
| 652 | + ): |
| 653 | + if isinstance(obj, datetime.datetime): |
| 654 | + # simplejson and ujson do not use sep=T |
| 655 | + expected = expected.replace("T", " ") |
| 656 | + |
| 657 | + elif obj is MultiEnum.BYTES or obj == b"some-bytes": |
| 658 | + expected = "some-bytes" |
| 659 | + |
| 660 | + elif obj is MultiEnum: |
| 661 | + expected = list(expected) |
| 662 | + expected[4] = "some-bytes" |
| 663 | + |
619 | 664 | ## Test |
620 | 665 | env.set_formatter(class_()) |
621 | 666 | extra = { |
|
0 commit comments