diff options
| author | 2019-03-27 21:09:23 +0530 | |
|---|---|---|
| committer | 2019-03-27 21:09:23 +0530 | |
| commit | 463e7f9d757ce8142f23f67f55f3c41d4336ba56 (patch) | |
| tree | fc3e6484b77907bc0158c271af26261d013ae508 /bot/__init__.py | |
| parent | Merge pull request #99 from python-discord/config-update (diff) | |
| parent | Merge pull request #132 from python-discord/dpy-cog-changes (diff) | |
Merge pull request #1 from python-discord/master
syncing fork
Diffstat (limited to 'bot/__init__.py')
| -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') | 
