-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
38 lines (29 loc) · 910 Bytes
/
logger.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
import json
import sys
from typing import TYPE_CHECKING
from loguru import logger
from context import current_context
if TYPE_CHECKING:
from loguru import Message, Record
def setup_logger() -> None:
logger.remove()
logger.add(_sink)
logger.configure(patcher=_patcher)
def _patcher(message: "Record") -> None:
"""Copy values from thread context to extra field.
Original extra data has higher priority than context data.
"""
message["extra"] = dict(current_context, **message["extra"])
def _sink(message: "Message") -> None:
print(
json.dumps(
{
"time": message.record["time"].isoformat(),
"level": message.record["level"].name,
"message": message.record["message"],
"extra": message.record["extra"],
},
default=str,
),
file=sys.stdout,
)