|
1 | 1 | # This example shows how the logger can be set up to use a custom JSON format.
|
2 |
| -import logging |
3 | 2 | import json
|
4 |
| -import traceback |
5 |
| -from datetime import datetime |
6 |
| -import copy |
7 |
| -import json_logging |
| 3 | +import logging |
8 | 4 | import sys
|
9 | 5 |
|
10 |
| -json_logging.ENABLE_JSON_LOGGING = True |
| 6 | +import json_logging |
11 | 7 |
|
12 | 8 |
|
13 | 9 | def extra(**kw):
|
14 | 10 | '''Add the required nested props layer'''
|
15 | 11 | return {'extra': {'props': kw}}
|
16 | 12 |
|
17 | 13 |
|
18 |
| -class CustomJSONLog(logging.Formatter): |
| 14 | +class CustomJSONLog(json_logging.JSONLogFormatter): |
19 | 15 | """
|
20 | 16 | Customized logger
|
21 | 17 | """
|
22 | 18 |
|
23 |
| - def get_exc_fields(self, record): |
24 |
| - if record.exc_info: |
25 |
| - exc_info = self.format_exception(record.exc_info) |
26 |
| - else: |
27 |
| - exc_info = record.exc_text |
28 |
| - return {'python.exc_info': exc_info} |
29 |
| - |
30 |
| - @classmethod |
31 |
| - def format_exception(cls, exc_info): |
32 |
| - return ''.join(traceback.format_exception(*exc_info)) if exc_info else '' |
33 |
| - |
34 | 19 | def format(self, record):
|
35 |
| - json_log_object = {"@timestamp": datetime.utcnow().isoformat(), |
36 |
| - "level": record.levelname, |
37 |
| - "message": record.getMessage(), |
38 |
| - "caller": record.filename + '::' + record.funcName |
39 |
| - } |
40 |
| - json_log_object['data'] = { |
41 |
| - "python.logger_name": record.name, |
42 |
| - "python.module": record.module, |
43 |
| - "python.funcName": record.funcName, |
44 |
| - "python.filename": record.filename, |
45 |
| - "python.lineno": record.lineno, |
46 |
| - "python.thread": record.threadName, |
47 |
| - "python.pid": record.process |
48 |
| - } |
49 |
| - if hasattr(record, 'props'): |
50 |
| - json_log_object['data'].update(record.props) |
51 |
| - |
52 |
| - if record.exc_info or record.exc_text: |
53 |
| - json_log_object['data'].update(self.get_exc_fields(record)) |
| 20 | + json_customized_log_object = ({ |
| 21 | + "customized_prop": "customized value", |
| 22 | + "message": record.getMessage() |
| 23 | + }) |
54 | 24 |
|
55 |
| - return json.dumps(json_log_object) |
| 25 | + return json.dumps(json_customized_log_object) |
56 | 26 |
|
57 | 27 |
|
58 | 28 | # You would normally import logger_init and setup the logger in your main module - e.g.
|
59 | 29 | # main.py
|
60 | 30 |
|
61 |
| -json_logging.init_non_web(custom_formatter=CustomJSONLog) |
| 31 | +json_logging.init_non_web(custom_formatter=CustomJSONLog, enable_json=True) |
62 | 32 |
|
63 | 33 | logger = logging.getLogger(__name__)
|
64 | 34 | logger.setLevel(logging.DEBUG)
|
65 | 35 | logger.addHandler(logging.StreamHandler(sys.stderr))
|
66 | 36 |
|
67 |
| -logger.info('Starting') |
68 |
| -try: |
69 |
| - 1 / 0 |
70 |
| -except: # noqa pylint: disable=bare-except |
71 |
| - logger.exception('You can\'t divide by zero') |
| 37 | +logger.info('sample log message') |
0 commit comments