diff options
author | 2019-03-25 12:56:16 -0400 | |
---|---|---|
committer | 2019-03-25 12:56:16 -0400 | |
commit | b8bceb22e17f1b8dd8bd25668545301d44bbb96d (patch) | |
tree | c67d6991aded8def18c885b857511e5c2ea054fd | |
parent | Merge pull request #152 from bramz/feature/124/magic-8ball (diff) | |
parent | Add TRACE logging level (Fixes #149) (diff) |
Merge pull request #150 from python-discord/fix-trace-logging
Add TRACE logging level (Fixes #149)
-rw-r--r-- | bot/__init__.py | 38 |
1 files changed, 29 insertions, 9 deletions
diff --git a/bot/__init__.py b/bot/__init__.py index dc97df3d..54b242ee 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -1,3 +1,4 @@ +import logging import logging.handlers import os from pathlib import Path @@ -6,25 +7,44 @@ import arrow from bot.constants import Client -# start datetime + +# Configure the "TRACE" logging level (e.g. "log.trace(message)") +logging.TRACE = 5 +logging.addLevelName(logging.TRACE, "TRACE") + + +def monkeypatch_trace(self, msg, *args, **kwargs): + """ + 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 + +# Set timestamp of when execution started (approximately) start_time = arrow.utcnow() -# set up logging +# Set up file logging log_dir = Path("bot", "log") log_file = log_dir / "hackbot.log" os.makedirs(log_dir, exist_ok=True) -# file handler sets up rotating logs every 5 MB +# File handler rotates logs every 5 MB file_handler = logging.handlers.RotatingFileHandler( log_file, maxBytes=5*(2**20), backupCount=10) -file_handler.setLevel(logging.DEBUG) +file_handler.setLevel(logging.TRACE if Client.debug else logging.DEBUG) -# console handler prints to terminal +# Console handler prints to terminal console_handler = logging.StreamHandler() -level = logging.DEBUG if Client.debug else logging.INFO +level = logging.TRACE if Client.debug else logging.INFO console_handler.setLevel(level) -# remove old loggers if any +# Remove old loggers, if any root = logging.getLogger() if root.handlers: for handler in root.handlers: @@ -34,11 +54,11 @@ if root.handlers: logging.getLogger("discord").setLevel(logging.ERROR) logging.getLogger("websockets").setLevel(logging.ERROR) -# setup new logging configuration +# Setup new logging configuration logging.basicConfig( format='%(asctime)s - %(name)s %(levelname)s: %(message)s', datefmt="%D %H:%M:%S", - level=logging.DEBUG, + level=logging.TRACE if Client.debug else logging.DEBUG, handlers=[console_handler, file_handler] ) logging.getLogger().info('Logging initialization complete') |