diff options
Diffstat (limited to 'bot')
| -rw-r--r-- | bot/__init__.py | 63 | ||||
| -rw-r--r-- | bot/log.py | 63 | 
2 files changed, 65 insertions, 61 deletions
| diff --git a/bot/__init__.py b/bot/__init__.py index c6a48105..b64f4732 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -6,80 +6,21 @@ except ModuleNotFoundError:      pass  import asyncio -import logging -import logging.handlers  import os  from functools import partial, partialmethod -from pathlib import Path  import arrow  from discord.ext import commands +from bot import log  from bot.command import Command -from bot.constants import Client  from bot.group import Group - -# Configure the "TRACE" logging level (e.g. "log.trace(message)") -logging.TRACE = 5 -logging.addLevelName(logging.TRACE, "TRACE") - - -def monkeypatch_trace(self: logging.Logger, 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(logging.TRACE): -        self._log(logging.TRACE, msg, args, **kwargs) - - -logging.Logger.trace = monkeypatch_trace +log.setup()  # Set timestamp of when execution started (approximately)  start_time = arrow.utcnow() -# Set up file logging -log_dir = Path("bot/log") -log_file = log_dir / "hackbot.log" -os.makedirs(log_dir, exist_ok=True) - -# File handler rotates logs every 5 MB -file_handler = logging.handlers.RotatingFileHandler( -    log_file, maxBytes=5 * (2**20), backupCount=10, encoding="utf-8", -) -file_handler.setLevel(logging.TRACE if Client.debug else logging.DEBUG) - -# Console handler prints to terminal -console_handler = logging.StreamHandler() -level = logging.TRACE if Client.debug else logging.INFO -console_handler.setLevel(level) - -# Remove old loggers, if any -root = logging.getLogger() -if root.handlers: -    for handler in root.handlers: -        root.removeHandler(handler) - -# Silence irrelevant loggers -logging.getLogger("discord").setLevel(logging.ERROR) -logging.getLogger("websockets").setLevel(logging.ERROR) -logging.getLogger("PIL").setLevel(logging.ERROR) -logging.getLogger("matplotlib").setLevel(logging.ERROR) -logging.getLogger("async_rediscache").setLevel(logging.WARNING) - -# Setup new logging configuration -logging.basicConfig( -    format="%(asctime)s - %(name)s %(levelname)s: %(message)s", -    datefmt="%D %H:%M:%S", -    level=logging.TRACE if Client.debug else logging.DEBUG, -    handlers=[console_handler, file_handler], -) -logging.getLogger().info("Logging initialization complete") - -  # On Windows, the selector event loop is required for aiodns.  if os.name == "nt":      asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) diff --git a/bot/log.py b/bot/log.py new file mode 100644 index 00000000..1f4b9159 --- /dev/null +++ b/bot/log.py @@ -0,0 +1,63 @@ +import logging +import logging.handlers +import os +from pathlib import Path + +from bot.constants import Client + + +def setup() -> None: +    """Set up loggers.""" +    # Configure the "TRACE" logging level (e.g. "log.trace(message)") +    logging.TRACE = 5 +    logging.addLevelName(logging.TRACE, "TRACE") +    logging.Logger.trace = _monkeypatch_trace + +    # Set up file logging +    log_dir = Path("bot/log") +    log_file = log_dir / "hackbot.log" +    os.makedirs(log_dir, exist_ok=True) + +    # File handler rotates logs every 5 MB +    file_handler = logging.handlers.RotatingFileHandler( +        log_file, maxBytes=5 * (2 ** 20), backupCount=10, encoding="utf-8", +    ) +    file_handler.setLevel(logging.TRACE if Client.debug else logging.DEBUG) + +    # Console handler prints to terminal +    console_handler = logging.StreamHandler() +    level = logging.TRACE if Client.debug else logging.INFO +    console_handler.setLevel(level) + +    # Remove old loggers, if any +    root = logging.getLogger() +    if root.handlers: +        for handler in root.handlers: +            root.removeHandler(handler) + +    # Silence irrelevant loggers +    logging.getLogger("discord").setLevel(logging.ERROR) +    logging.getLogger("websockets").setLevel(logging.ERROR) +    logging.getLogger("PIL").setLevel(logging.ERROR) +    logging.getLogger("matplotlib").setLevel(logging.ERROR) +    logging.getLogger("async_rediscache").setLevel(logging.WARNING) + +    # Setup new logging configuration +    logging.basicConfig( +        format="%(asctime)s - %(name)s %(levelname)s: %(message)s", +        datefmt="%D %H:%M:%S", +        level=logging.TRACE if Client.debug else logging.DEBUG, +        handlers=[console_handler, file_handler], +    ) +    logging.getLogger().info("Logging initialization complete") + + +def _monkeypatch_trace(self: logging.Logger, 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(logging.TRACE): +        self._log(logging.TRACE, msg, args, **kwargs) | 
