-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathlogs.py
More file actions
80 lines (60 loc) · 2.04 KB
/
logs.py
File metadata and controls
80 lines (60 loc) · 2.04 KB
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
import logging
from json import dumps
from typing import Any
import structlog
from structlog import WriteLoggerFactory
from config_reader import LogConfig, LogRenderer
def get_structlog_config(log_config: LogConfig) -> dict[str, Any]:
"""
Build structlog configuration dictionary.
Args:
log_config: Log configuration object
Returns:
Configuration dict for structlog.configure()
"""
min_level = logging.DEBUG if log_config.show_debug_logs else 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:
"""
Build list of structlog processors.
Args:
log_config: Log configuration object
Returns:
List of processor functions
"""
def custom_json_serializer(data: dict, *args, **kwargs) -> str:
"""Custom JSON serializer with ordered keys."""
result = {}
if log_config.show_datetime and "timestamp" in data:
result["timestamp"] = data.pop("timestamp")
for key in ("level", "event"):
if key in data:
result[key] = data.pop(key)
result.update(**data)
return dumps(result, default=str)
processors = []
if log_config.show_datetime:
processors.append(
structlog.processors.TimeStamper(
fmt=log_config.datetime_format,
utc=log_config.time_in_utc,
)
)
processors.append(structlog.processors.add_log_level)
if log_config.renderer == LogRenderer.JSON:
processors.append(
structlog.processors.JSONRenderer(serializer=custom_json_serializer)
)
else:
processors.append(
structlog.dev.ConsoleRenderer(
colors=log_config.use_colors_in_console,
pad_level=True,
)
)
return processors