-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.py
114 lines (94 loc) Β· 3.49 KB
/
log.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""Custom logger."""
import json
from os import path
from sys import stdout
from loguru import logger
from config import settings
def json_formatter(record: dict) -> str:
"""
Pass raw log to be serialized.
:param dict record: Dictionary containing logged message with metadata.
:returns: str
"""
def serialize(log: dict) -> str:
"""
Parse log message into Datadog JSON format.
:param dict log: Dictionary containing logged message with metadata.
:returns: str
"""
subset = {
"time": log["time"].strftime("%m/%d/%Y, %H:%M:%S"),
"message": log["message"],
"level": log["level"].name,
"function": log.get("function"),
"module": log.get("name"),
}
if log.get("exception", None):
subset.update({"exception": log["exception"]})
return json.dumps(subset)
record["extra"]["serialized"] = serialize(record)
return "{extra[serialized]},\n"
def log_formatter(record: dict) -> str:
"""
Formatter for .log records
:param dict record: Key/value object containing log message & metadata.
:returns: str
"""
if record["level"].name == "TRACE":
return "<fg #5278a3>{time:MM-DD-YYYY HH:mm:ss}</fg #5278a3> | <fg #d2eaff>{level}</fg #d2eaff>: <light-white>{message}</light-white>\n"
if record["level"].name == "INFO":
return "<fg #5278a3>{time:MM-DD-YYYY HH:mm:ss}</fg #5278a3> | <fg #98bedf>{level}</fg #98bedf>: <light-white>{message}</light-white>\n"
if record["level"].name == "WARNING":
return "<fg #5278a3>{time:MM-DD-YYYY HH:mm:ss}</fg #5278a3> | <fg #b09057>{level}</fg #b09057>: <light-white>{message}</light-white>\n"
if record["level"].name == "SUCCESS":
return "<fg #5278a3>{time:MM-DD-YYYY HH:mm:ss}</fg #5278a3> | <fg #6dac77>{level}</fg #6dac77>: <light-white>{message}</light-white>\n"
if record["level"].name == "ERROR":
return "<fg #5278a3>{time:MM-DD-YYYY HH:mm:ss}</fg #5278a3> | <fg #a35252>{level}</fg #a35252>: <light-white>{message}</light-white>\n"
if record["level"].name == "CRITICAL":
return "<fg #5278a3>{time:MM-DD-YYYY HH:mm:ss}</fg #5278a3> | <fg #521010>{level}</fg #521010>: <light-white>{message}</light-white>\n"
return "<fg #5278a3>{time:MM-DD-YYYY HH:mm:ss}</fg #5278a3> | <fg #98bedf>{level}</fg #98bedf>: <light-white>{message}</light-white>\n"
def create_logger() -> logger:
"""
Configure custom logger.
:returns: logger
"""
logger.remove()
logger.add(
stdout,
colorize=True,
catch=True,
level="TRACE",
format=log_formatter,
)
if settings.ENVIRONMENT == "production" and path.isdir("/var/log/api"):
# Datadog JSON logs
logger.add(
"/var/log/api/info.json",
format=json_formatter,
rotation="200 MB",
level="TRACE",
compression="zip",
)
# Readable logs
logger.add(
"/var/log/api/info.log",
colorize=True,
catch=True,
level="TRACE",
format=log_formatter,
rotation="200 MB",
compression="zip",
)
else:
logger.add(
"./logs/error.log",
colorize=True,
catch=True,
format=log_formatter,
rotation="200 MB",
compression="zip",
level="ERROR",
)
return logger
# Custom logger
LOGGER = create_logger()