diff options
-rw-r--r-- | bot/exts/moderation/watchchannels/_watchchannel.py | 3 | ||||
-rw-r--r-- | bot/log.py | 59 |
2 files changed, 11 insertions, 51 deletions
diff --git a/bot/exts/moderation/watchchannels/_watchchannel.py b/bot/exts/moderation/watchchannels/_watchchannel.py index fb75d525d..98097c40d 100644 --- a/bot/exts/moderation/watchchannels/_watchchannel.py +++ b/bot/exts/moderation/watchchannels/_watchchannel.py @@ -12,6 +12,7 @@ from discord.ext.commands import Cog, Context from pydis_core.site_api import ResponseCodeError from pydis_core.utils import scheduling from pydis_core.utils.channel import get_or_fetch_channel +from pydis_core.utils.logging import CustomLogger from pydis_core.utils.members import get_or_fetch_member from bot.bot import Bot @@ -19,7 +20,7 @@ from bot.constants import BigBrother as BigBrotherConfig, Guild as GuildConfig, from bot.exts.filtering._filters.unique.discord_token import DiscordTokenFilter from bot.exts.filtering._filters.unique.webhook import WEBHOOK_URL_RE from bot.exts.moderation.modlog import ModLog -from bot.log import CustomLogger, get_logger +from bot.log import get_logger from bot.pagination import LinePaginator from bot.utils import CogABCMeta, messages, time diff --git a/bot/log.py b/bot/log.py index 8b18df70a..068c77189 100644 --- a/bot/log.py +++ b/bot/log.py @@ -1,63 +1,29 @@ import logging import os import sys -from logging import Logger, handlers +from logging import handlers from pathlib import Path -from typing import TYPE_CHECKING, cast import coloredlogs import sentry_sdk +from pydis_core.utils import logging as core_logging from sentry_sdk.integrations.logging import LoggingIntegration from sentry_sdk.integrations.redis import RedisIntegration from bot import constants -TRACE_LEVEL = 5 - - -if TYPE_CHECKING: - LoggerClass = Logger -else: - LoggerClass = logging.getLoggerClass() - - -class CustomLogger(LoggerClass): - """Custom implementation of the `Logger` class with an added `trace` method.""" - - def trace(self, msg: str, *args, **kwargs) -> None: - """ - Log 'msg % args' with severity 'TRACE'. - - To pass exception information, use the keyword argument exc_info with - a true value, e.g. - - logger.trace("Houston, we have an %s", "interesting problem", exc_info=1) - """ - if self.isEnabledFor(TRACE_LEVEL): - self.log(TRACE_LEVEL, msg, *args, **kwargs) - - -def get_logger(name: str | None = None) -> CustomLogger: - """Utility to make mypy recognise that logger is of type `CustomLogger`.""" - return cast(CustomLogger, logging.getLogger(name)) +get_logger = core_logging.get_logger def setup() -> None: """Set up loggers.""" - logging.TRACE = TRACE_LEVEL - logging.addLevelName(TRACE_LEVEL, "TRACE") - logging.setLoggerClass(CustomLogger) - root_log = get_logger() - format_string = "%(asctime)s | %(name)s | %(levelname)s | %(message)s" - log_format = logging.Formatter(format_string) - if constants.FILE_LOGS: log_file = Path("logs", "bot.log") log_file.parent.mkdir(exist_ok=True) file_handler = handlers.RotatingFileHandler(log_file, maxBytes=5242880, backupCount=7, encoding="utf8") - file_handler.setFormatter(log_format) + file_handler.setFormatter(core_logging.log_format) root_log.addHandler(file_handler) if "COLOREDLOGS_LEVEL_STYLES" not in os.environ: @@ -69,18 +35,11 @@ def setup() -> None: } if "COLOREDLOGS_LOG_FORMAT" not in os.environ: - coloredlogs.DEFAULT_LOG_FORMAT = format_string + coloredlogs.DEFAULT_LOG_FORMAT = core_logging.log_format._fmt - coloredlogs.install(level=TRACE_LEVEL, logger=root_log, stream=sys.stdout) + coloredlogs.install(level=core_logging.TRACE_LEVEL, logger=root_log, stream=sys.stdout) root_log.setLevel(logging.DEBUG if constants.DEBUG_MODE else logging.INFO) - get_logger("discord").setLevel(logging.WARNING) - get_logger("websockets").setLevel(logging.WARNING) - get_logger("chardet").setLevel(logging.WARNING) - get_logger("async_rediscache").setLevel(logging.WARNING) - - # Set back to the default of INFO even if asyncio's debug mode is enabled. - get_logger("asyncio").setLevel(logging.INFO) _set_trace_loggers() @@ -121,13 +80,13 @@ def _set_trace_loggers() -> None: level_filter = constants.Bot.trace_loggers if level_filter: if level_filter.startswith("*"): - get_logger().setLevel(TRACE_LEVEL) + get_logger().setLevel(core_logging.TRACE_LEVEL) elif level_filter.startswith("!"): - get_logger().setLevel(TRACE_LEVEL) + get_logger().setLevel(core_logging.TRACE_LEVEL) for logger_name in level_filter.strip("!,").split(","): get_logger(logger_name).setLevel(logging.DEBUG) else: for logger_name in level_filter.strip(",").split(","): - get_logger(logger_name).setLevel(TRACE_LEVEL) + get_logger(logger_name).setLevel(core_logging.TRACE_LEVEL) |