-
Notifications
You must be signed in to change notification settings - Fork 1
/
log_utils.py
60 lines (47 loc) · 1.89 KB
/
log_utils.py
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
import json
import logging
from typing import Any
from logwood import global_config
from logwood.handlers.logging import StreamHandler
_OMIT = {'__dict__', '__class__', '__dir__', 'levelno', 'levelname',
'exc_info', 'stack_info', 'request', 'msg', 'args', 'message'}
class StreamFormatterHandler(StreamHandler):
def __init__(self, *args, **kwargs):
hostname = kwargs.pop('hostname')
self.format_message = self._format_message_json if hostname != 'localhost' else self._format_message_simple
super().__init__(*args, **kwargs)
@staticmethod
def _apply_args(record: dict[str, Any]):
if 'args' in record and record['args']:
record['message'] %= record['args']
del record['args']
@classmethod
def _format_message_json(cls, record: dict[str, Any]) -> str:
cls._apply_args(record)
return json.dumps(record)
@classmethod
def _format_message_simple(cls, record: dict[str, Any]) -> str:
cls._apply_args(record)
return global_config.default_format % record
class JsonFormatter(logging.Formatter):
""" slightly more usable than json-log-formatter """
def __init__(self):
super().__init__()
def format(self, record):
""" copy & paste from standard logger """
message = record.getMessage()
data = {
'message': message,
'level': record.levelname,
'severity': record.levelname,
'timestamp': record.created,
'timestamp_str': str(record.created)
}
if record.exc_info:
record.exc_text = self.formatException(record.exc_info)
if record.stack_info:
data['stack_info'] = self.formatStack(record.stack_info)
for attr, value in record.__dict__.items():
if attr not in _OMIT:
data[attr] = value
return json.dumps(data, default=str)