|
|
@@ -27,7 +27,7 @@ Additionally, there are functions used in config loading: |
|
|
|
import json |
|
|
|
import logging |
|
|
|
import logging.handlers |
|
|
|
from os import path |
|
|
|
from os import mkdir, path |
|
|
|
|
|
|
|
import blowfish |
|
|
|
|
|
|
@@ -59,26 +59,36 @@ def _setup_logging(): |
|
|
|
|
|
|
|
if metadata.get("enableLogging"): |
|
|
|
hand = logging.handlers.TimedRotatingFileHandler |
|
|
|
fmt = "[%(asctime)s %(levelname)-8s] %(name)s: %(message)s" |
|
|
|
formatter = logging.Formatter(fmt=fmt, datefmt="%Y-%m-%d %H:%M:%S") |
|
|
|
formatter = BotFormatter() |
|
|
|
color_formatter = BotFormatter(color=True) |
|
|
|
|
|
|
|
logfile = lambda f: path.join(log_dir, f) |
|
|
|
|
|
|
|
if not path.isdir(log_dir): |
|
|
|
if not path.exists(log_dir): |
|
|
|
mkdir(log_dir, 0700) |
|
|
|
else: |
|
|
|
msg = "log_dir ({0}) exists but is not a directory!" |
|
|
|
print msg.format(log_dir) |
|
|
|
exit(1) |
|
|
|
|
|
|
|
main_handler = hand(logfile("bot.log"), "midnight", 1, 7) |
|
|
|
error_handler = hand(logfile("error.log"), "W6", 1, 4) |
|
|
|
debug_handler = hand(logfile("debug.log"), "H", 1, 6) |
|
|
|
stream_handler = logging.StreamHandler() |
|
|
|
|
|
|
|
main_handler.setLevel(logging.INFO) |
|
|
|
error_handler.setLevel(logging.WARNING) |
|
|
|
debug_handler.setLevel(logging.DEBUG) |
|
|
|
stream_handler.setLevel(logging.DEBUG) |
|
|
|
|
|
|
|
handlers = (main_handler, error_handler, debug_handler, stream_handler) |
|
|
|
for h in handlers: |
|
|
|
for h in (main_handler, error_handler, debug_handler): |
|
|
|
h.setFormatter(formatter) |
|
|
|
logger.addHandler(h) |
|
|
|
|
|
|
|
stream_handler = logging.StreamHandler() |
|
|
|
stream_handler.setLevel(logging.DEBUG) |
|
|
|
stream_handler.setFormatter(color_formatter) |
|
|
|
logger.addHandler(stream_handler) |
|
|
|
|
|
|
|
else: |
|
|
|
logger.addHandler(logging.NullHandler()) |
|
|
|
|
|
|
@@ -134,7 +144,7 @@ def load(): |
|
|
|
|
|
|
|
def decrypt(key): |
|
|
|
"""Use the key to decrypt passwords in our config file. |
|
|
|
|
|
|
|
|
|
|
|
Call this if load() returns True. Catch password decryption errors and |
|
|
|
report them to the user. |
|
|
|
""" |
|
|
@@ -189,3 +199,30 @@ def schedule(minute, hour, month_day, month, week_day): |
|
|
|
pass |
|
|
|
|
|
|
|
return tasks |
|
|
|
|
|
|
|
|
|
|
|
class BotFormatter(logging.Formatter): |
|
|
|
def __init__(self, color=False): |
|
|
|
self._format = super(BotFormatter, self).format |
|
|
|
if color: |
|
|
|
fmt = "[%(asctime)s %(lvl)s] %(name)s: %(message)s" |
|
|
|
self.format = lambda record: self._format(self.format_color(record)) |
|
|
|
else: |
|
|
|
fmt = "[%(asctime)s %(levelname)-8s] %(name)s: %(message)s" |
|
|
|
self.format = self._format |
|
|
|
datefmt = "%Y-%m-%d %H:%M:%S" |
|
|
|
super(BotFormatter, self).__init__(fmt=fmt, datefmt=datefmt) |
|
|
|
|
|
|
|
def format_color(self, record): |
|
|
|
l = record.levelname.ljust(8) |
|
|
|
if record.levelno == logging.DEBUG: |
|
|
|
record.lvl = l.join(("\x1b[37m", "\x1b[0m")) |
|
|
|
if record.levelno == logging.INFO: |
|
|
|
record.lvl = l.join(("\x1b[32m", "\x1b[0m")) |
|
|
|
if record.levelno == logging.WARNING: |
|
|
|
record.lvl = l.join(("\x1b[36m", "\x1b[0m")) |
|
|
|
if record.levelno == logging.ERROR: |
|
|
|
record.lvl = l.join(("\x1b[33m", "\x1b[0m")) |
|
|
|
if record.levelno == logging.CRITICAL: |
|
|
|
record.lvl = l.join(("\x1b[31m", "\x1b[0m")) |
|
|
|
return record |