2021-10-01 01:18:04 +00:00
|
|
|
'''基于logging的日志器。'''
|
2021-07-07 16:00:24 +00:00
|
|
|
import logging
|
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-01-20 13:06:06 +00:00
|
|
|
|
2022-01-18 12:32:43 +00:00
|
|
|
factory = logging.getLogRecordFactory()
|
2022-05-08 06:32:42 +00:00
|
|
|
basic_logger_format = "[%(asctime)s][%(botname)s][%(levelname)s][%(pathname)s:%(lineno)d]: %(message)s"
|
2021-07-07 16:00:24 +00:00
|
|
|
|
2022-01-18 12:46:56 +00:00
|
|
|
|
2022-01-18 12:32:43 +00:00
|
|
|
def record_factory(*args, **kwargs):
|
|
|
|
record = factory(*args, **kwargs)
|
|
|
|
for x in sys.path:
|
|
|
|
record.pathname = record.pathname.replace(x, '')
|
|
|
|
record.pathname = record.pathname.replace('\\', '.').replace('/', '.')[1:]
|
2022-01-18 12:46:56 +00:00
|
|
|
record.botname = re.split(r'[/\\]', sys.path[0])[-1]
|
2022-01-18 12:32:43 +00:00
|
|
|
return record
|
2021-07-07 16:00:24 +00:00
|
|
|
|
|
|
|
|
2022-01-18 12:32:43 +00:00
|
|
|
logging.setLogRecordFactory(record_factory)
|
2021-07-07 16:00:24 +00:00
|
|
|
|
|
|
|
|
2022-01-18 12:32:43 +00:00
|
|
|
class Logginglogger:
|
2022-01-20 13:06:06 +00:00
|
|
|
def __init__(self, fmt=basic_logger_format, **kwargs):
|
2021-07-07 16:00:24 +00:00
|
|
|
logging.basicConfig(
|
2021-08-07 03:59:58 +00:00
|
|
|
format=fmt,
|
2021-08-21 07:45:21 +00:00
|
|
|
level=logging.INFO if not kwargs.get("debug") else logging.DEBUG
|
2021-07-07 16:00:24 +00:00
|
|
|
)
|
2021-10-10 14:05:19 +00:00
|
|
|
self.log = logging.getLogger('akaribot.logger')
|
|
|
|
self.log.setLevel(logging.INFO)
|
2021-07-07 16:00:24 +00:00
|
|
|
|
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
|
2021-07-16 15:50:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
Logger = Logginglogger()
|