Py-Hanabi/log_setup/logger_setup.py

61 lines
2.1 KiB
Python
Raw Permalink Normal View History

import logging
import verboselogs
2023-03-15 15:42:22 +01:00
class LoggerManager:
def __init__(self, console_level: int = logging.INFO):
self.logger = verboselogs.VerboseLogger("hanab-suite")
2023-03-15 15:42:22 +01:00
self.logger.setLevel(logging.DEBUG)
2023-03-15 15:42:22 +01:00
self.file_formatter = logging.Formatter(
'[%(asctime)s] [PID %(process)s] [%(levelname)7s]: %(message)s'
)
2023-03-15 15:42:22 +01:00
self.info_file_formatter = logging.Formatter(
'[%(asctime)s] [PID %(process)s]: %(message)s'
)
2023-05-14 14:04:42 +02:00
self.console_formatter = logging.Formatter(
'[%(levelname)7s]: %(message)s'
)
self.nothing_formatter = logging.Formatter(
'%(message)s'
)
self.console_handler = logging.StreamHandler()
self.console_handler.setLevel(console_level)
self.console_handler.setFormatter(self.nothing_formatter)
self.debug_file_handler = logging.FileHandler("debug_log.txt")
self.debug_file_handler.setFormatter(self.file_formatter)
self.debug_file_handler.setLevel(logging.DEBUG)
self.verbose_file_handler = logging.FileHandler("verbose_log.txt")
self.verbose_file_handler.setFormatter(self.file_formatter)
self.verbose_file_handler.setLevel(verboselogs.VERBOSE)
self.info_file_handler = logging.FileHandler("log.txt")
self.info_file_handler.setFormatter(self.info_file_formatter)
self.info_file_handler.setLevel(logging.INFO)
self.logger.addHandler(self.console_handler)
self.logger.addHandler(self.debug_file_handler)
self.logger.addHandler(self.verbose_file_handler)
self.logger.addHandler(self.info_file_handler)
def set_console_level(self, level: int):
self.console_handler.setLevel(level)
if level == logging.INFO:
self.console_handler.setFormatter(self.nothing_formatter)
else:
self.console_handler.setFormatter(self.console_formatter)
def is_console_level_active(self, level: int):
return level >= self.console_handler.level
def get_logger(self):
return self.logger