-
Notifications
You must be signed in to change notification settings - Fork 1
/
log_config.py
44 lines (37 loc) · 1.26 KB
/
log_config.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
import logging
import logging.config
from datetime import datetime, timezone
class UTCFormatter(logging.Formatter):
def formatTime(self, record, datefmt=None):
dt = datetime.fromtimestamp(record.created, tz=timezone.utc)
t = dt.strftime("%Y-%m-%d %H:%M:%S")
return f"{t} UTC"
logging_config = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"utc": {
"format": " %(asctime)s %(levelname)-8s %(name)-12s %(message)s",
"()": UTCFormatter, # special nomenclature to use custom formatter
},
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"level": "INFO",
"formatter": "utc", # Use the UTCFormatter here
"stream": "ext://sys.stderr",
},
},
"root": {
"level": "INFO",
"handlers": ["console"],
},
"loggers": {},
}
# Explicitly set the formatter for the root logger
logging.getLogger().handlers[0].setFormatter(logging_config["formatters"]["utc"])
for logger_name, logger_config in logging_config["loggers"].items():
logger_config["handlers"] = ["console"]
logger_config["propagate"] = False # Avoid double logging
logging.config.dictConfig(logging_config)