update
This commit is contained in:
1 parent
4a53051fee
commit
039ec3cf52
179 files changed
+22667
No files matched your search
@@ -0,0 +1,89 @@
|
||||
import os
|
||||
import sys
|
||||
import shutil
|
||||
import logging
|
||||
from logging import INFO, ERROR, DEBUG, CRITICAL, FATAL, WARNING, WARN, NOTSET
|
||||
|
||||
import datetime
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
|
||||
|
||||
#The background is set with 40 plus the number of the color, and the foreground with 30
|
||||
|
||||
#These are the sequences need to get colored ouput
|
||||
RESET_SEQ = "\033[0m"
|
||||
COLOR_SEQ = "\033[1;%dm"
|
||||
BOLD_SEQ = "\033[1m"
|
||||
|
||||
COLORS = {
|
||||
'WARNING': YELLOW,
|
||||
'INFO': WHITE,
|
||||
'DEBUG': BLUE,
|
||||
'CRITICAL': RED,
|
||||
'ERROR': RED
|
||||
}
|
||||
|
||||
class Logger(logging.Logger):
|
||||
def __init__(self, path_output, path_config=None, postfix=None, level=logging.NOTSET ):
|
||||
logging.Logger.__init__(self, '', level)
|
||||
|
||||
# logger filename suffix
|
||||
suffix = f"{datetime.datetime.now().strftime('%Y%m%d-%HH%MM%SS')}"
|
||||
if postfix is not None:
|
||||
suffix += str("-" + postfix)
|
||||
|
||||
# make dir & set filename
|
||||
if path_config is not None:
|
||||
basename = path_config.name.split(".")[0]
|
||||
self.path_output = Path(path_output).joinpath(basename, suffix)
|
||||
self.path_output.mkdir(parents=True, exist_ok=True)
|
||||
filename = self.path_output.joinpath(f"logger.log")
|
||||
shutil.copyfile(src=path_config, dst=self.path_output.joinpath(path_config.name))
|
||||
else:
|
||||
self.path_output = Path(path_output)
|
||||
self.path_output.mkdir(parents=True, exist_ok=True)
|
||||
filename = self.path_output.joinpath(suffix + ".log")
|
||||
|
||||
# add file hander
|
||||
fHander = logging.FileHandler(filename)
|
||||
fFormat = logging.Formatter('[%(asctime)s][%(levelname)s] %(message)s')
|
||||
fHander.setFormatter(fFormat)
|
||||
self.addHandler(fHander)
|
||||
|
||||
# add console hander
|
||||
cHander = logging.StreamHandler(sys.stdout)
|
||||
cFormat = ColoredFormatter( RESET_SEQ+"[%(asctime)s] %(message)s"+RESET_SEQ )
|
||||
cHander.setFormatter(cFormat)
|
||||
self.addHandler(cHander)
|
||||
|
||||
def setContext(self, key, value):
|
||||
self.context[key] = value
|
||||
|
||||
def log(self, msg, level, *args, **kwargs):
|
||||
extra = kwargs
|
||||
extra.update(self.context)
|
||||
# super(Logger, self).info(msg)
|
||||
|
||||
def error(self, msg, *args, **kwargs):
|
||||
super(Logger, self).error(msg, *args, **kwargs)
|
||||
|
||||
def exception(self, *args, **kwargs):
|
||||
e = sys.exc_info()
|
||||
extra = kwargs
|
||||
extra.update(self.context)
|
||||
super(Logger, self).error(e[1].message)
|
||||
|
||||
|
||||
class ColoredFormatter(logging.Formatter):
|
||||
def __init__(self, msg, use_color = True):
|
||||
logging.Formatter.__init__(self, msg, datefmt='%Y-%m-%d %H:%M:%S')
|
||||
self.use_color = use_color
|
||||
|
||||
def format(self, record):
|
||||
levelname = record.levelname
|
||||
if self.use_color and levelname in COLORS:
|
||||
levelname_color = COLOR_SEQ % (30+COLORS[levelname]) + levelname + RESET_SEQ
|
||||
record.levelname = levelname_color
|
||||
return logging.Formatter.format(self, record)
|
||||
Reference in new issue
Block a user