forked from bobbui/json-logging-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_log_format.py
More file actions
35 lines (23 loc) · 902 Bytes
/
custom_log_format.py
File metadata and controls
35 lines (23 loc) · 902 Bytes
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
# This example shows how the logger can be set up to use a custom JSON format.
import json
import logging
import sys
import json_logging
import json_logging.formatters
def extra(**kw):
"""Add the required nested props layer"""
return {"extra": {"props": kw}}
class CustomJSONLog(json_logging.formatters.JSONLogFormatter):
"""
Customized logger
"""
def format(self, record):
json_customized_log_object = {"customized_prop": "customized value", "message": record.getMessage()}
return json.dumps(json_customized_log_object)
# You would normally import logger_init and setup the logger in your main module - e.g.
# main.py
json_logging.init_non_web(custom_formatter=CustomJSONLog, enable_json=True)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler(sys.stderr))
logger.info("sample log message")