-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
43 lines (37 loc) · 1.09 KB
/
common.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
from logging import Filter
from flask import has_request_context, g
class LogContextFilter(Filter):
def filter(self, record):
if has_request_context() and hasattr(g, 'log_context'):
for k, v in g.log_context.items():
setattr(record, k, v)
return True
def get_logging_config(filters=None):
return {
'version': 1,
'formatters': {
'json': {
'class': 'pythonjsonlogger.jsonlogger.JsonFormatter',
'format': '%(name)s %(message)s'
},
},
'handlers': {
'wsgi': {
'class': 'logging.StreamHandler',
'stream': 'ext://flask.logging.wsgi_errors_stream',
'formatter': 'json',
'filters': filters,
'level': 'INFO',
},
},
'filters': {
'context': {
'()': 'common.LogContextFilter',
}
},
'root': {
'level': 'INFO',
'propagate': True,
'handlers': ['wsgi'],
},
}