PaiGram/logger.py

56 lines
1.7 KiB
Python
Raw Normal View History

2022-04-14 07:18:45 +00:00
import logging
2022-05-24 12:15:17 +00:00
from logging.handlers import RotatingFileHandler
import colorlog
2022-04-14 07:18:45 +00:00
import os
2022-05-24 12:15:17 +00:00
current_path = os.path.realpath(os.getcwd())
2022-05-27 03:38:27 +00:00
log_path = os.path.join(current_path, "logs")
2022-04-14 07:18:45 +00:00
if not os.path.exists(log_path):
2022-05-24 12:15:17 +00:00
os.mkdir(log_path)
2022-05-27 03:38:27 +00:00
log_file_name = os.path.join(log_path, "log.log")
2022-04-14 07:18:45 +00:00
log_colors_config = {
2022-05-27 03:38:27 +00:00
"DEBUG": "cyan",
"INFO": "green",
"WARNING": "yellow",
"ERROR": "red",
"CRITICAL": "red",
2022-04-14 07:18:45 +00:00
}
class Logger:
def __init__(self):
self.logger = logging.getLogger("TGPaimonBot")
2022-05-27 03:38:27 +00:00
root_logger = logging.getLogger()
root_logger.setLevel(logging.CRITICAL)
2022-04-14 07:18:45 +00:00
self.logger.setLevel(logging.INFO)
self.formatter = colorlog.ColoredFormatter(
2022-05-27 03:38:27 +00:00
"%(log_color)s[%(asctime)s] [%(levelname)s] - %(message)s", log_colors=log_colors_config)
self.formatter2 = logging.Formatter("[%(asctime)s] [%(levelname)s] - %(message)s")
fh = RotatingFileHandler(filename=log_file_name, maxBytes=1024 * 1024 * 5, backupCount=5,
2022-05-27 03:38:27 +00:00
encoding="utf-8")
2022-04-14 07:18:45 +00:00
fh.setFormatter(self.formatter2)
2022-05-27 03:38:27 +00:00
root_logger.addHandler(fh)
2022-04-14 07:18:45 +00:00
ch = colorlog.StreamHandler()
ch.setFormatter(self.formatter)
2022-05-27 03:38:27 +00:00
root_logger.addHandler(ch)
2022-04-14 07:18:45 +00:00
2022-05-27 03:38:27 +00:00
def getLogger(self):
return self.logger
2022-04-14 07:18:45 +00:00
def debug(self, msg, exc_info=None):
2022-05-27 03:38:27 +00:00
self.logger.debug(msg=msg, exc_info=exc_info)
2022-04-14 07:18:45 +00:00
def info(self, msg, exc_info=None):
2022-05-27 03:38:27 +00:00
self.logger.info(msg=msg, exc_info=exc_info)
2022-04-14 07:18:45 +00:00
def warning(self, msg, exc_info=None):
2022-05-27 03:38:27 +00:00
self.logger.warning(msg=msg, exc_info=exc_info)
2022-04-14 07:18:45 +00:00
def error(self, msg, exc_info=None):
2022-05-27 03:38:27 +00:00
self.logger.error(msg=msg, exc_info=exc_info)
2022-04-14 07:18:45 +00:00
Log = Logger()