You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Python logging library to emit JSON log that can be easily indexed and searchable by logging infrastructure such as [ELK](https://www.elastic.co/elk-stack), [EFK](https://www.digitalocean.com/community/tutorials/how-to-set-up-an-elasticsearch-fluentd-and-kibana-efk-logging-stack-on-kubernetes), AWS Cloudwatch, GCP Stackdriver
3
2
4
-
If you're using Cloud Foundry, it might worth to check out the library [SAP/cf-python-logging-support](https://github.com/SAP/cf-python-logging-support) which I'm also original author.
3
+
Python logging library to emit JSON log that can be easily indexed and searchable by logging infrastructure such
2. Lightweight, no dependencies, minimal configuration needed (1 LoC to get it working)
24
33
3. Seamlessly integrate with Python native **logging** module. Support both Python 2.7.x and 3.x
25
34
4. Auto extract **correlation-id** for distributed tracing [\[1\]](#1-what-is-correlation-idrequest-id)
26
-
5. Support HTTP request instrumentation. Built in support for [FastAPI](https://fastapi.tiangolo.com/), [Flask](https://github.com/pallets/flask/), [Sanic](https://github.com/channelcat/sanic), [Quart](https://gitlab.com/pgjones/quart), [Connexion](https://github.com/zalando/connexion). Extensible to support other web frameworks. PR welcome :smiley: .
27
-
6. Highly customizable: support inject arbitrary extra properties to JSON log message, override logging formatter, etc.
35
+
5. Support HTTP request instrumentation. Built in support for [FastAPI](https://fastapi.tiangolo.com/)
, [Quart](https://gitlab.com/pgjones/quart), [Connexion](https://github.com/zalando/connexion). Extensible to support
38
+
other web frameworks. PR welcome :smiley: .
39
+
6. Highly customizable: support inject arbitrary extra properties to JSON log message, override logging formatter, etc.
28
40
7. Production ready, has been used in production since 2017
29
41
30
42
# 2. Usage
43
+
31
44
Install by running this command:
32
-
> pip install json-logging
45
+
> pip install json-logging
33
46
34
-
By default log will be emitted in normal format to ease the local development. To enable it on production set enable_json in init_\<framework name\>(enable_json=True) method call (set **json_logging.ENABLE_JSON_LOGGING** or **ENABLE_JSON_LOGGING environment variable** to true is not recommended and will be deprecated in future versions).
47
+
By default log will be emitted in normal format to ease the local development. To enable it on production set
48
+
enable_json in init_\<framework name\>(enable_json=True) method call (set **json_logging.ENABLE_JSON_LOGGING** or **
49
+
ENABLE_JSON_LOGGING environment variable** to true is not recommended and will be deprecated in future versions).
35
50
36
-
To configure, call **json_logging.init_< framework_name >()**. Once configured library will try to configure all loggers (existing and newly created) to emit log in JSON format.
51
+
To configure, call **json_logging.init_< framework_name >()**. Once configured library will try to configure all
52
+
loggers (existing and newly created) to emit log in JSON format.
If you want to use root logger as main logger to emit log. Made sure you call **config_root_logger()** after initialize root logger (by logging.basicConfig() or logging.getLogger()) [\[2\]](#2-python-logging-propagate)
261
+
262
+
If you want to use root logger as main logger to emit log. Made sure you call **config_root_logger()** after initialize
263
+
root logger (by logging.basicConfig() or logging.getLogger()) [\[2\]](#2-python-logging-propagate)
264
+
207
265
```python
208
266
logging.basicConfig()
209
-
json_logging.init_<framework name >()
267
+
json_logging.init_ < framework
268
+
name > ()
210
269
json_logging.config_root_logger()
211
270
```
212
271
213
272
## 2.6 Custom log formatter
214
-
Customer JSON log formatter can be passed to init method. see examples for more detail: [non web](https://github.com/thangbn/json-logging-python/blob/master/example/custom_log_format.py),
logging library can be configured by setting the value in json_logging, all configuration must be placed before json_logging.init method call
288
+
289
+
logging library can be configured by setting the value in json_logging, all configuration must be placed before
290
+
json_logging.init method call
227
291
228
292
Name | Description | Default value
229
293
--- | --- | ---
230
-
ENABLE_JSON_LOGGING | **DEPRECATED** Whether to enable JSON logging mode.Can be set as an environment variable, enable when set to to either one in following list (case-insensitive) **['true', '1', 'y', 'yes']** , this have no effect on request logger | false
294
+
ENABLE_JSON_LOGGING | **
295
+
DEPRECATED** Whether to enable JSON logging mode.Can be set as an environment variable, enable when set to to either one in following list (case-insensitive) **['true', '1', 'y', 'yes']** , this have no effect on request logger | false
231
296
CORRELATION_ID_HEADERS | List of HTTP headers that will be used to look for correlation-id value. HTTP headers will be searched one by one according to list order| ['X-Correlation-ID','X-Request-ID']
232
297
EMPTY_VALUE | Default value when a logging record property is None | '-'
233
298
CORRELATION_ID_GENERATOR | function to generate unique correlation-id | uuid.uuid1
@@ -242,7 +307,11 @@ CREATE_CORRELATION_ID_IF_NOT_EXISTS | Whether to generate a new correlation-id
242
307
TODO: update Python API docs on Github page
243
308
244
309
# 5. Framework support plugin development
245
-
To add support for a new web framework, you need to extend following classes in [**framework_base**](/blob/master/json_logging/framework_base.py) and register support using [**json_logging.register_framework_support**](https://github.com/thangbn/json-logging-python/blob/master/json_logging/__init__.py#L38) method:
310
+
311
+
To add support for a new web framework, you need to extend following classes in [**
312
+
framework_base**](/blob/master/json_logging/framework_base.py) and register support using [**
@@ -251,9 +320,12 @@ ResponseAdapter | Helper class help to extract logging-relevant information from
251
320
FrameworkConfigurator | Class to perform logging configuration for given framework as needed | no
252
321
AppRequestInstrumentationConfigurator | Class to perform request instrumentation logging configuration | no
253
322
254
-
Take a look at [**json_logging/base_framework.py**](json_logging/framework_base.py), [**json_logging.flask**](json_logging/framework/flask) and [**json_logging.sanic**](json_logging/framework/sanic) packages for reference implementations.
323
+
Take a look at [**json_logging/base_framework.py**](json_logging/framework_base.py), [**
324
+
json_logging.flask**](json_logging/framework/flask) and [**json_logging.sanic**](json_logging/framework/sanic) packages
325
+
for reference implementations.
255
326
256
327
# 6. FAQ & Troubleshooting
328
+
257
329
1. I configured everything, but no logs are printed out?
258
330
259
331
- Forgot to add handlers to your logger?
@@ -262,13 +334,17 @@ Take a look at [**json_logging/base_framework.py**](json_logging/framework_base.
262
334
2. Same log statement is printed out multiple times.
263
335
264
336
- Check whether the same handler is added to both parent and child loggers [2]
265
-
- If you using flask, by default option **use_reloader** is set to **True** which will start 2 instances of web application. change it to False to disable this behaviour [\[3\]](#3-more-on-flask-use-reloader)
337
+
- If you using flask, by default option **use_reloader** is set to **True** which will start 2 instances of web
338
+
application. change it to False to disable this behaviour [\[3\]](#3-more-on-flask-use-reloader)
266
339
267
340
# 7. References
341
+
268
342
## [0] Full logging format references
343
+
269
344
2 types of logging statement will be emitted by this library:
270
-
- Application log: normal logging statement
271
-
e.g.:
345
+
346
+
- Application log: normal logging statement e.g.:
347
+
272
348
```
273
349
{
274
350
"type": "log",
@@ -288,7 +364,10 @@ e.g.:
288
364
"msg": "This is a message"
289
365
}
290
366
```
291
-
- Request log: request instrumentation logging statement which recorded request information such as response time, request size, etc.
367
+
368
+
- Request log: request instrumentation logging statement which recorded request information such as response time,
369
+
request size, etc.
370
+
292
371
```
293
372
{
294
373
"type": "request",
@@ -316,7 +395,9 @@ e.g.:
316
395
"response_sent_at": "2017-12-23T16:55:37.280Z"
317
396
}
318
397
```
398
+
319
399
See following tables for detail format explanation:
400
+
320
401
- Common field
321
402
322
403
Field | Description | Format | Example
@@ -362,10 +443,14 @@ referer | For HTTP requests, identifies the address of the webpage (i.e. the URI
362
443
x_forwarded_for | Comma-separated list of IP addresses, the left-most being the original client, followed by proxy server addresses that forwarded the client request. | string | 192.0.2.60,10.12.9.23
0 commit comments