print timestamp on stdout too

This commit is contained in:
əlemi 2022-04-28 12:54:23 +02:00
parent cfd5dd079a
commit 7c4c523562
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E

View file

@ -9,14 +9,14 @@ def configure_logging(name:str, level=logging.INFO, color:bool = True):
from logging.handlers import RotatingFileHandler from logging.handlers import RotatingFileHandler
class ColorFormatter(logging.Formatter): class ColorFormatter(logging.Formatter):
def __init__(self, fmt:str): def __init__(self, fmt:str, datefmt:str=None):
self.fmt : str = fmt self.fmt : str = fmt
self.formatters : Dict[int, logging.Formatter] = { self.formatters : Dict[int, logging.Formatter] = {
logging.DEBUG: logging.Formatter(colored(fmt, color='grey')), logging.DEBUG: logging.Formatter(colored(fmt, color='grey'), datefmt),
logging.INFO: logging.Formatter(colored(fmt)), logging.INFO: logging.Formatter(colored(fmt), datefmt),
logging.WARNING: logging.Formatter(colored(fmt, color='yellow')), logging.WARNING: logging.Formatter(colored(fmt, color='yellow'), datefmt),
logging.ERROR: logging.Formatter(colored(fmt, color='red')), logging.ERROR: logging.Formatter(colored(fmt, color='red'), datefmt),
logging.CRITICAL: logging.Formatter(colored(fmt, color='red', attrs=['bold'])), logging.CRITICAL: logging.Formatter(colored(fmt, color='red', attrs=['bold']), datefmt),
} }
def format(self, record:logging.LogRecord) -> str: def format(self, record:logging.LogRecord) -> str:
@ -33,8 +33,8 @@ def configure_logging(name:str, level=logging.INFO, color:bool = True):
ch = logging.StreamHandler() ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG) ch.setLevel(logging.DEBUG)
# create formatter and add it to the handlers # create formatter and add it to the handlers
file_formatter = logging.Formatter("[%(asctime)s] [%(name)s] [%(levelname)s] %(message)s", "%b %d %Y %H:%M:%S") file_formatter = logging.Formatter("[%(asctime)s.%(msecs)03d] [%(name)s] [%(levelname)s] %(message)s", "%b %d %Y %H:%M:%S")
print_formatter = ColorFormatter("> %(message)s") if color else logging.Formatter("> %(message)s") print_formatter = ColorFormatter("%(asctime)s| %(message)s", "%H:%M:%S") if color else logging.Formatter("> %(message)s")
fh.setFormatter(file_formatter) fh.setFormatter(file_formatter)
ch.setFormatter(print_formatter) ch.setFormatter(print_formatter)
# add the handlers to the logger # add the handlers to the logger