-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlogs.py
88 lines (72 loc) · 2.59 KB
/
logs.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import logging
from json import dumps
import structlog
from structlog import WriteLoggerFactory
from config_reader import LogConfig, LogRenderer
def get_structlog_config(
log_config: LogConfig
) -> dict:
"""
Get config for structlog
:param log_config: объект LogConfig with log parameters
:return: dict with structlog config
"""
# Show debug level logs?
if log_config.show_debug_logs is True:
min_level = logging.DEBUG
else:
min_level = logging.INFO
return {
"processors": get_processors(log_config),
"cache_logger_on_first_use": True,
"wrapper_class": structlog.make_filtering_bound_logger(min_level),
"logger_factory": WriteLoggerFactory()
}
def get_processors(log_config: LogConfig) -> list:
"""
Returns processors list for structlog
:param log_config: LogConfig object with log parameters
:return: processors list for structlog
"""
def custom_json_serializer(data, *args, **kwargs):
"""
JSON-objects custom serializer
"""
result = dict()
if log_config.show_datetime is True:
result["timestamp"] = data.pop("timestamp")
# Other two keys goes in this order
for key in ("level", "event"):
if key in data:
result[key] = data.pop(key)
# Remaining keys will be printed "as is"
# (usually in alphabet order)
result.update(**data)
return dumps(result, default=str)
processors = list()
# In some cases there is no need to print a timestamp,
# because it is already added by an upstream service, such as systemd
if log_config.show_datetime is True:
processors.append(structlog.processors.TimeStamper(
fmt=log_config.datetime_format,
utc=log_config.time_in_utc
)
)
# Always add a log level
processors.append(structlog.processors.add_log_level)
# Render selection: JSON or for output to terminal
if log_config.renderer == LogRenderer.JSON:
processors.append(structlog.processors.JSONRenderer(serializer=custom_json_serializer))
else:
processors.append(structlog.dev.ConsoleRenderer(
# You can turn off colors in the logs
colors=log_config.use_colors_in_console,
# You can remove padding in levels, i.e. instead of
# [info ] Some info log
# [warning] Some warning log
# will be
# [info] Some info log
# [warning] Some warning log
pad_level=True
))
return processors