-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.py
38 lines (31 loc) · 1.01 KB
/
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 logging
import sys
from typing import Dict
class StreamToLogger(object):
"""
Fake file-like stream object that redirects writes to a logger instance.
"""
def __init__(self, logger, log_level=logging.INFO):
self.logger = logger
self.log_level = log_level
self.linebuf = ''
def write(self, buf):
for line in buf.rstrip().splitlines():
self.logger.log(self.log_level, line.rstrip())
def flush(self):
pass
log_level = "DEBUG"
def get_logger(name:str) -> logging.Logger:
log = logging.getLogger(name)
log.setLevel(log_level)
formatter = logging.Formatter(fmt="%(asctime)s %(levelname)-8s %(name)s: %(message)s",
datefmt="%Y-%m-%d - %H:%M:%S")
ch = logging.StreamHandler()
ch.setFormatter(formatter)
log.addHandler(ch)
# sl = StreamToLogger(log)
# sys.stdout = sl
return log
def set_log_level_from_config(config:Dict):
global log_level
log_level = config["logger"]["level"]