-
Notifications
You must be signed in to change notification settings - Fork 16
/
logging.py
42 lines (31 loc) · 1.26 KB
/
logging.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
import json
import logging
import platform
from django.core.serializers.json import DjangoJSONEncoder
from django.http import UnreadablePostError
def skip_unreadable_post(record):
if record.exc_info:
exc_type, exc_value = record.exc_info[:2]
if isinstance(exc_value, UnreadablePostError):
return False
return True
class AppendExtraJSONHandler(logging.StreamHandler):
DEFAULT_STREAM_HANDLER_VARIABLE_KEYS = {
'name', 'msg', 'args', 'levelname', 'levelno', 'pathname', 'filename', 'module', 'exc_info', 'exc_text',
'stack_info', 'lineno', 'funcName', 'created', 'msecs', 'relativeCreated', 'thread', 'threadName',
'processName', 'process',
}
CUSTOM_STREAM_HANDLER_VARIABLE_KEYS = {'hostname'}
def emit(self, record):
extra = {
k: v
for k, v in record.__dict__.items()
if k not in self.DEFAULT_STREAM_HANDLER_VARIABLE_KEYS.union(self.CUSTOM_STREAM_HANDLER_VARIABLE_KEYS)
}
record.msg = '{} --- {}'.format(record.msg, json.dumps(extra, cls=DjangoJSONEncoder))
super().emit(record)
class HostnameFilter(logging.Filter):
hostname = platform.node()
def filter(self, record):
record.hostname = self.hostname
return True