Skip to content

Commit

Permalink
fix: loguru now handles discord.py logs
Browse files Browse the repository at this point in the history
  • Loading branch information
qLunar committed Apr 23, 2024
1 parent 1bc27a4 commit 785e5af
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion chiya/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import glob
import os
import sys
import logging

import discord
from discord.ext import commands
Expand Down Expand Up @@ -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 = "<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan> | <level>{message}</level>"
log.add(sys.stdout, format=fmt, level=log_level)
log.add(os.path.join("logs", "bot.log"), format=fmt, rotation="1 day")
Expand Down

0 comments on commit 785e5af

Please sign in to comment.