|
| 1 | +<!-- [](https://pypi.python.org/pypi/python-json-logger/) |
| 2 | +[](https://pypi.python.org/pypi/python-json-logger/) |
| 3 | +[](https://github.com/nhairs/python-json-logger) --> |
| 4 | +[](https://github.com/nhairs/python-json-logger) |
1 | 5 | 
|
2 |
| -[](https://pypi.python.org/pypi/python-json-logger/) |
3 |
| -[](https://pypi.python.org/pypi/python-json-logger/) |
4 |
| - |
| 6 | +# |
5 | 7 | # Python JSON Logger
|
6 | 8 |
|
7 |
| -This library is provided to allow standard python logging to output log data as json objects. With JSON we can make our logs more readable by machines and we can stop writing custom parsers for syslog type records. |
| 9 | +Python JSON Logger enables you produce JSON logs when using Python's `logging` package. |
| 10 | + |
| 11 | +JSON logs are machine readable allowing for much easier parsing and ingestion into log aggregation tools. |
8 | 12 |
|
9 | 13 |
|
10 | 14 | ### 🚨 Important 🚨
|
11 | 15 |
|
12 | 16 | This repository is a maintained fork of [madzak/python-json-logger](https://github.com/madzak/python-json-logger) pending [a PEP 541 request](https://github.com/pypi/support/issues/3607) for the PyPI package. The future direction of the project is being discussed [here](https://github.com/nhairs/python-json-logger/issues/1).
|
13 | 17 |
|
14 |
| -[**Changelog**](https://github.com/nhairs/python-json-logger/blob/main/CHANGELOG.md) |
15 |
| - |
16 |
| -## Installation |
17 |
| - |
18 |
| -Note: All versions of this fork use version `>=3.0.0` - to use pre-fork versions use `python-json-logger<3.0.0`. |
19 |
| - |
20 |
| -### Install via pip |
21 |
| - |
22 |
| -Until the PEP 541 request is complete you will need to install directly from github. |
23 |
| - |
24 |
| -#### Install from GitHub |
25 |
| - |
26 |
| -To install from releases: |
27 |
| - |
28 |
| -```shell |
29 |
| -# e.g. 3.0.0 wheel |
30 |
| -pip install 'python-json-logger@https://github.com/nhairs/python-json-logger/releases/download/v3.0.0/python_json_logger-3.0.0-py3-none-any.whl' |
31 |
| -``` |
32 |
| - |
33 |
| -To install from head: |
34 |
| - |
35 |
| -```shell |
36 |
| -pip install 'python-json-logger@git+https://github.com/nhairs/python-json-logger.git' |
37 |
| -``` |
38 |
| - |
39 |
| -To install a specific version from a tag: |
40 |
| - |
41 |
| -```shell |
42 |
| -# Last released version before forking |
43 |
| -pip install 'python-json-logger@git+https://github.com/nhairs/python-json-logger.git@v2.0.7' |
44 |
| -``` |
45 |
| - |
46 |
| -#### Install from Source |
47 |
| - |
48 |
| -```shell |
49 |
| -git clone https://github.com/nhairs/python-json-logger.git |
50 |
| -cd python-json-logger |
51 |
| -pip install -e . |
52 |
| -``` |
53 |
| - |
54 |
| -## Usage |
55 |
| - |
56 |
| -Python JSON Logger provides `logging.Formatter`s that encode the logged message into JSON. Although a variety of JSON encoders are supported, in the following examples we will use the `pythonjsonlogger.json.JsonFormatter` which uses the the `json` module from the standard library. |
57 |
| - |
58 |
| -### Integrating with Python's logging framework |
59 |
| - |
60 |
| -To produce JSON output, attach the formatter to a logging handler: |
61 |
| - |
62 |
| -```python |
63 |
| - import logging |
64 |
| - from pythonjsonlogger.json import JsonFormatter |
65 |
| - |
66 |
| - logger = logging.getLogger() |
67 |
| - |
68 |
| - logHandler = logging.StreamHandler() |
69 |
| - formatter = JsonFormatter() |
70 |
| - logHandler.setFormatter(formatter) |
71 |
| - logger.addHandler(logHandler) |
72 |
| -``` |
73 |
| - |
74 |
| -### Output fields |
75 |
| - |
76 |
| -You can control the logged fields by setting the `fmt` argument when creating the formatter. By default formatters will follow the same `style` of `fmt` as the `logging` module: `%`, `$`, and `{`. All [`LogRecord` attributes](https://docs.python.org/3/library/logging.html#logrecord-attributes) can be output using their name. |
77 |
| - |
78 |
| -```python |
79 |
| -formatter = JsonFormatter("{message}{asctime}{exc_info}", style="{") |
80 |
| -``` |
81 |
| - |
82 |
| -You can also add extra fields to your json output by specifying a dict in place of message, as well as by specifying an `extra={}` argument. |
83 |
| - |
84 |
| -Contents of these dictionaries will be added at the root level of the entry and may override basic fields. |
85 |
| - |
86 |
| -You can also use the `add_fields` method to add to or generally normalize the set of default set of fields, it is called for every log event. For example, to unify default fields with those provided by [structlog](http://www.structlog.org/) you could do something like this: |
87 |
| - |
88 |
| -```python |
89 |
| -class CustomJsonFormatter(JsonFormatter): |
90 |
| - def add_fields(self, log_record, record, message_dict): |
91 |
| - super().add_fields(log_record, record, message_dict) |
92 |
| - if not log_record.get('timestamp'): |
93 |
| - # this doesn't use record.created, so it is slightly off |
94 |
| - now = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%fZ') |
95 |
| - log_record['timestamp'] = now |
96 |
| - if log_record.get('level'): |
97 |
| - log_record['level'] = log_record['level'].upper() |
98 |
| - else: |
99 |
| - log_record['level'] = record.levelname |
100 |
| - return |
101 |
| - |
102 |
| -formatter = CustomJsonFormatter('%(timestamp)s %(level)s %(name)s %(message)s') |
103 |
| -``` |
104 |
| - |
105 |
| -Items added to the log record will be included in *every* log message, no matter what the format requires. |
106 |
| - |
107 |
| -You can also override the `process_log_record` method to modify fields before they are serialized to JSON. |
108 |
| - |
109 |
| -```python |
110 |
| -class SillyFormatter(JsonFormatter): |
111 |
| - def process_log_record(log_record): |
112 |
| - new_record = {k[::-1]: v for k, v in log_record.items()} |
113 |
| - return new_record |
114 |
| -``` |
115 |
| - |
116 |
| -#### Supporting custom styles |
117 |
| - |
118 |
| -It is possible to support custom `style`s by setting `validate=False` and overriding the `parse` method. |
119 |
| - |
120 |
| -For example: |
121 |
| - |
122 |
| -```python |
123 |
| -class CommaSupport(JsonFormatter): |
124 |
| - def parse(self) -> list[str]: |
125 |
| - if isinstance(self._style, str) and self._style == ",": |
126 |
| - return self._fmt.split(",") |
127 |
| - return super().parse() |
128 |
| - |
129 |
| -formatter = CommaSupport("message,asctime", style=",", validate=False) |
130 |
| -``` |
131 |
| - |
132 |
| -### Custom object serialization |
133 |
| - |
134 |
| -Most formatters support `json_default` which is used to control how objects are serialized. |
135 |
| - |
136 |
| -For custom handling of object serialization you can specify default json object translator or provide a custom encoder |
137 |
| - |
138 |
| -```python |
139 |
| -def my_default(obj): |
140 |
| - if isinstance(obj, MyClass): |
141 |
| - return {"special": obj.special} |
142 |
| - |
143 |
| -formatter = JsonFormatter(json_default=my_default) |
144 |
| -``` |
145 |
| - |
146 |
| -### Using a Config File |
147 |
| - |
148 |
| -To use the module with a config file using the [`fileConfig` function](https://docs.python.org/3/library/logging.config.html#logging.config.fileConfig), use the class `pythonjsonlogger.json.JsonFormatter`. Here is a sample config file. |
149 |
| - |
150 |
| -```ini |
151 |
| -[loggers] |
152 |
| -keys = root,custom |
153 |
| - |
154 |
| -[logger_root] |
155 |
| -handlers = |
156 |
| - |
157 |
| -[logger_custom] |
158 |
| -level = INFO |
159 |
| -handlers = custom |
160 |
| -qualname = custom |
161 |
| - |
162 |
| -[handlers] |
163 |
| -keys = custom |
164 |
| - |
165 |
| -[handler_custom] |
166 |
| -class = StreamHandler |
167 |
| -level = INFO |
168 |
| -formatter = json |
169 |
| -args = (sys.stdout,) |
170 |
| - |
171 |
| -[formatters] |
172 |
| -keys = json |
173 |
| - |
174 |
| -[formatter_json] |
175 |
| -format = %(message)s |
176 |
| -class = pythonjsonlogger.jsonlogger.JsonFormatter |
177 |
| -``` |
178 |
| - |
179 |
| -### Alternate JSON Encoders |
180 |
| - |
181 |
| -The following JSON encoders are also supported: |
182 |
| - |
183 |
| -- [orjson](https://github.com/ijl/orjson) - `pythonjsonlogger.orjon.OrjsonFormatter` |
184 |
| -- [msgspec](https://github.com/jcrist/msgspec) - `pythonjsonlogger.msgspec.MsgspecFormatter` |
185 |
| - |
186 |
| -## Example Output |
187 |
| - |
188 |
| -Sample JSON with a full formatter (basically the log message from the unit test). Every log message will appear on 1 line like a typical logger. |
| 18 | +## Documentation |
189 | 19 |
|
190 |
| -```json |
191 |
| -{ |
192 |
| - "threadName": "MainThread", |
193 |
| - "name": "root", |
194 |
| - "thread": 140735202359648, |
195 |
| - "created": 1336281068.506248, |
196 |
| - "process": 41937, |
197 |
| - "processName": "MainProcess", |
198 |
| - "relativeCreated": 9.100914001464844, |
199 |
| - "module": "tests", |
200 |
| - "funcName": "testFormatKeys", |
201 |
| - "levelno": 20, |
202 |
| - "msecs": 506.24799728393555, |
203 |
| - "pathname": "tests/tests.py", |
204 |
| - "lineno": 60, |
205 |
| - "asctime": "12-05-05 22:11:08,506248", |
206 |
| - "message": "testing logging format", |
207 |
| - "filename": "tests.py", |
208 |
| - "levelname": "INFO", |
209 |
| - "special": "value", |
210 |
| - "run": 12 |
211 |
| -} |
212 |
| -``` |
| 20 | +- [Documentation](https://nhairs.github.io/python-json-logger/latest/) |
| 21 | +- [Quickstart Guide](https://nhairs.github.io/python-json-logger/latest/quickstart/) |
| 22 | +- [Change Log](https://nhairs.github.io/python-json-logger/latest/changelog/) |
| 23 | +- [Contributing](https://nhairs.github.io/python-json-logger/latest/contributing/) |
213 | 24 |
|
214 | 25 | ## License
|
215 | 26 |
|
|
0 commit comments