diff --git a/chiya/bot.py b/chiya/bot.py index 28faa7cb..fd27c54e 100644 --- a/chiya/bot.py +++ b/chiya/bot.py @@ -2,6 +2,7 @@ import glob import os import sys +import logging import discord from discord.ext import commands @@ -46,8 +47,24 @@ async def setup_logger(): log_level = config["bot"]["log_level"] if not log_level: log_level = "NOTSET" - log.remove() + + class InterceptHandler(logging.Handler): + """ + Setup up an Interceptor class to redirect all logs from the standard logging library to loguru. + """ + def emit(self, record: logging.LogRecord) -> None: + # Get corresponding Loguru level if it exists. + level: str | int + try: + level = log.level(record.levelname).name + except ValueError: + level = record.levelno + + log.opt(exception=record.exc_info).log(level, record.getMessage()) + + discord.utils.setup_logging(handler=InterceptHandler(),level=logging.getLevelName(config["bot"]["log_level"]), root=False) + fmt = "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {name} | {message}" log.add(sys.stdout, format=fmt, level=log_level) log.add(os.path.join("logs", "bot.log"), format=fmt, rotation="1 day")