From 785e5af4cac6e34ed550ab631dbc780e4e231aba Mon Sep 17 00:00:00 2001 From: qLunar <100940270+qLunar@users.noreply.github.com> Date: Tue, 23 Apr 2024 13:00:02 +0530 Subject: [PATCH] fix: loguru now handles discord.py logs --- chiya/bot.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) 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")