aboutsummaryrefslogtreecommitdiffstats
path: root/bot/__init__.py
diff options
context:
space:
mode:
authorGravatar Christopher Goes <[email protected]>2019-03-20 20:54:33 -0600
committerGravatar Christopher Goes <[email protected]>2019-03-20 20:54:33 -0600
commit50017e51c25eec82b88e3dca52f163dd1bfce63a (patch)
tree32de7fa6a85c4e89eed40cd3e0907319c9fe7e7b /bot/__init__.py
parentMerge pull request #144 from MadisonMcdonald/encoding-fix (diff)
Add TRACE logging level (Fixes #149)
Diffstat (limited to 'bot/__init__.py')
-rw-r--r--bot/__init__.py38
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')