2018-08-05 11:13:25 +00:00
|
|
|
import logging, sys, time
|
|
|
|
|
|
|
|
class BitBotFormatter(logging.Formatter):
|
|
|
|
def formatTime(self, record, datefmt=None):
|
|
|
|
ct = self.converter(record.created)
|
|
|
|
if datefmt:
|
|
|
|
if "%f" in datefmt:
|
|
|
|
msec = "%03d" % record.msecs
|
|
|
|
datefmt = datefmt.replace("%f", msec)
|
|
|
|
s = time.strftime(datefmt, ct)
|
|
|
|
else:
|
|
|
|
t = time.strftime("%Y-%m-%d %H:%M:%S", ct)
|
|
|
|
s = "%s.%03d" % (t, record.msecs)
|
|
|
|
return s
|
2018-08-05 10:30:43 +00:00
|
|
|
|
|
|
|
class Module(object):
|
|
|
|
def __init__(self, bot):
|
|
|
|
self.logger = logging.getLogger(__name__)
|
|
|
|
self.logger.setLevel(logging.DEBUG)
|
|
|
|
|
2018-08-05 11:13:25 +00:00
|
|
|
formatter = BitBotFormatter(
|
2018-08-05 10:30:43 +00:00
|
|
|
"%(asctime)s - %(levelname)s - %(message)s",
|
2018-08-05 11:13:25 +00:00
|
|
|
"%Y-%m-%dT%H:%M:%S.%f%z")
|
2018-08-05 10:30:43 +00:00
|
|
|
|
|
|
|
stdout_handler = logging.StreamHandler(sys.stdout)
|
|
|
|
stdout_handler.setLevel(logging.INFO)
|
|
|
|
stdout_handler.setFormatter(formatter)
|
|
|
|
self.logger.addHandler(stdout_handler)
|
|
|
|
|
|
|
|
file_handler = logging.FileHandler('bot.log')
|
|
|
|
file_handler.setLevel(logging.DEBUG)
|
|
|
|
file_handler.setFormatter(formatter)
|
|
|
|
self.logger.addHandler(file_handler)
|
|
|
|
|
|
|
|
bot.events.on("log.debug").hook(self.debug)
|
|
|
|
bot.events.on("log.info").hook(self.info)
|
|
|
|
bot.events.on("log.warn").hook(self.warn)
|
|
|
|
bot.events.on("log.error").hook(self.error)
|
2018-08-05 10:54:24 +00:00
|
|
|
bot.events.on("log.critical").hook(self.critical)
|
2018-08-05 10:30:43 +00:00
|
|
|
|
|
|
|
def debug(self, event):
|
|
|
|
self._log(event, logging.DEBUG)
|
|
|
|
def info(self, event):
|
|
|
|
self._log(event, logging.INFO)
|
|
|
|
def warn(self, event):
|
|
|
|
self._log(event, logging.WARN)
|
|
|
|
def error(self, event):
|
|
|
|
self._log(event, logging.ERROR)
|
2018-08-05 10:52:50 +00:00
|
|
|
def critical(self, event):
|
|
|
|
self._log(event, logging.CRITICAL)
|
2018-08-05 10:30:43 +00:00
|
|
|
def _log(self, event, level):
|
|
|
|
message = event["message"]
|
|
|
|
params = event.get("params", [])
|
|
|
|
self.logger.log(level, message, *params)
|