-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathultrajson.py
More file actions
89 lines (65 loc) · 2.78 KB
/
Copy pathultrajson.py
File metadata and controls
89 lines (65 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""JSON Formatter using [ultrajson](https://github.com/ultrajson/ultrajson)"""
### IMPORTS
### ============================================================================
## Future
from __future__ import annotations
## Standard Library
from typing import Any, Optional, Callable
## Installed
## Application
from . import core
from . import defaults as d
from .utils import package_is_available
# We import ujson after checking it is available
package_is_available("ujson", throw_error=True)
import ujson # pylint: disable=wrong-import-position,wrong-import-order
### FUNCTIONS
### ============================================================================
def ujson_default(obj: Any) -> Any:
"""ujson default encoder function for non-standard types"""
if d.use_exception_default(obj):
return d.exception_default(obj)
if d.use_traceback_default(obj):
return d.traceback_default(obj)
if d.use_enum_default(obj):
return d.enum_default(obj)
if d.use_dataclass_default(obj):
return d.dataclass_default(obj)
if d.use_type_default(obj):
return d.type_default(obj)
return d.unknown_default(obj)
### CLASSES
### ============================================================================
class UltraJsonFormatter(core.BaseJsonFormatter):
"""JSON formatter using [ultrajson](https://github.com/ultrajson/ultrajson) (`ujson`) for encoding.
!!! warning "UltraJSON is in maintenance mode"
[Per README](https://github.com/ultrajson/ultrajson/tree/main?tab=readme-ov-file#project-status) users
are encouraged to move to another JSON encoder such as `orjson`.
!!! warning "Note that ultrajson handles certain input different to other encoders in python-json-logger."
`datetime.datetime` objects use `' '` as the delimiter instead of `'T'`.
`bytes` can only be encoded if they are valid `utf-8`.
New in `4.1`
"""
def __init__(
self,
*args,
json_default: Optional[Callable] = ujson_default,
json_indent: int = 0,
**kwargs,
) -> None:
"""
Args:
args: see [BaseJsonFormatter][pythonjsonlogger.core.BaseJsonFormatter]
json_default: a function for encoding non-standard objects
json_indent: indent output with this number of spaces.
kwargs: see [BaseJsonFormatter][pythonjsonlogger.core.BaseJsonFormatter]
"""
super().__init__(*args, **kwargs)
self.json_default = json_default
self.json_indent = json_indent
return
def jsonify_log_record(self, log_data: core.LogData) -> str:
"""Returns a json string of the log data."""
return ujson.dumps(
log_data, indent=self.json_indent, default=self.json_default, reject_bytes=False
)