2021-10-01 01:18:04 +00:00
|
|
|
'''基于logging的日志器。'''
|
2022-07-01 06:26:41 +00:00
|
|
|
import os
|
2022-01-18 12:46:56 +00:00
|
|
|
import re
|
2022-01-18 12:32:43 +00:00
|
|
|
import sys
|
2021-07-07 16:00:24 +00:00
|
|
|
|
2022-07-01 06:26:41 +00:00
|
|
|
from loguru import logger
|
2022-01-20 13:06:06 +00:00
|
|
|
|
2022-08-04 07:52:42 +00:00
|
|
|
from config import Config
|
|
|
|
|
|
|
|
debug = Config('debug')
|
|
|
|
|
2022-07-01 06:26:41 +00:00
|
|
|
logpath = os.path.abspath('./logs')
|
|
|
|
if not os.path.exists(logpath):
|
|
|
|
os.mkdir(logpath)
|
2022-01-18 12:46:56 +00:00
|
|
|
|
2022-07-01 06:26:41 +00:00
|
|
|
bot_name = re.split(r'[/\\]', sys.path[0])[-1].title()
|
2023-04-30 03:30:59 +00:00
|
|
|
basic_logger_format = "<cyan>[" + bot_name + \
|
|
|
|
"]</cyan><yellow>[{name}:{function}:{line}]</yellow><green>[{time:YYYY-MM-DD HH:mm:ss}]</green><level>[{level}]:{message}</level>"
|
2021-07-07 16:00:24 +00:00
|
|
|
|
|
|
|
|
2022-01-18 12:32:43 +00:00
|
|
|
class Logginglogger:
|
2022-07-01 06:26:41 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.log = logger
|
|
|
|
self.log.remove()
|
2022-08-04 07:52:42 +00:00
|
|
|
self.log.add(sys.stderr, format=basic_logger_format, level="DEBUG" if debug else "INFO", colorize=True)
|
2022-07-31 08:33:20 +00:00
|
|
|
self.log.add(logpath + '/' + bot_name + "_{time:YYYY-MM-DD}.log", format=basic_logger_format,
|
2023-01-12 07:51:03 +00:00
|
|
|
retention="10 days", encoding="utf8")
|
2022-01-18 12:32:43 +00:00
|
|
|
self.info = self.log.info
|
|
|
|
self.error = self.log.error
|
|
|
|
self.debug = self.log.debug
|
|
|
|
self.warn = self.log.warning
|
|
|
|
self.exception = self.log.exception
|
2022-08-04 07:52:42 +00:00
|
|
|
if debug:
|
|
|
|
self.log.warning("Debug mode is enabled.")
|
2021-07-16 15:50:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
Logger = Logginglogger()
|