forked from LiveCarta/BookConverter
improve loggers
This commit is contained in:
@@ -2,10 +2,30 @@ import os
|
||||
import logging
|
||||
|
||||
|
||||
class ColoredFormatter(logging.Formatter):
|
||||
MAPPING = {
|
||||
'DEBUG': 37, # white
|
||||
'INFO': 36, # cyan
|
||||
'WARNING': 33, # yellow
|
||||
'ERROR': 31, # red
|
||||
'CRITICAL': 41, # white on red bg
|
||||
}
|
||||
|
||||
PREFIX = '\033['
|
||||
SUFFIX = '\033[0m'
|
||||
|
||||
def __init__(self, patern):
|
||||
logging.Formatter.__init__(self, patern)
|
||||
|
||||
def format(self, record):
|
||||
seq = self.MAPPING.get(record.levelname, 37) # default white
|
||||
record.levelname = ('{0}{1}m{2}{3}') \
|
||||
.format(self.PREFIX, seq, record.levelname, self.SUFFIX)
|
||||
return logging.Formatter.format(self, record)
|
||||
|
||||
class BookLogger:
|
||||
def __init__(self, name, book_id, main_logger=None,
|
||||
filemode='w+', logging_level=logging.INFO,
|
||||
logging_format='%(asctime)s - %(levelname)s - %(message)s'):
|
||||
filemode='w+', logging_level=logging.INFO):
|
||||
"""
|
||||
Method for Logger configuration. Logger will write to file.
|
||||
:param name: name of the Logger.
|
||||
@@ -23,7 +43,7 @@ class BookLogger:
|
||||
filename = f'logs/{book_id}_log.log'
|
||||
file_path = os.path.join(folder_path, filename)
|
||||
file_handler = logging.FileHandler(file_path, mode=filemode)
|
||||
file_format = logging.Formatter(fmt=logging_format)
|
||||
file_format = ColoredFormatter('%(asctime)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)')
|
||||
file_handler.setFormatter(file_format)
|
||||
self.logger.addHandler(file_handler)
|
||||
self.logger.setLevel(logging_level)
|
||||
|
||||
Reference in New Issue
Block a user