2018-12-10 13:18:15 +00:00
|
|
|
import logging, logging.handlers, os, queue, sys, threading, time, typing
|
2018-08-05 11:13:25 +00:00
|
|
|
|
2018-10-01 12:48:22 +00:00
|
|
|
LEVELS = {
|
2018-10-01 15:04:41 +00:00
|
|
|
"trace": logging.DEBUG-1,
|
2018-10-01 12:48:22 +00:00
|
|
|
"debug": logging.DEBUG,
|
|
|
|
"info": logging.INFO,
|
|
|
|
"warn": logging.WARN,
|
|
|
|
"error": logging.ERROR,
|
|
|
|
"critical": logging.CRITICAL
|
|
|
|
}
|
|
|
|
|
2018-08-05 11:13:25 +00:00
|
|
|
class BitBotFormatter(logging.Formatter):
|
2018-10-30 18:03:44 +00:00
|
|
|
converter = time.gmtime
|
2018-08-05 11:13:25 +00:00
|
|
|
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
|
|
|
|
2018-08-28 11:23:57 +00:00
|
|
|
class Log(object):
|
2018-10-30 14:58:48 +00:00
|
|
|
def __init__(self, level: str, location: str):
|
2018-10-01 12:48:22 +00:00
|
|
|
logging.addLevelName(LEVELS["trace"], "TRACE")
|
2018-08-05 10:30:43 +00:00
|
|
|
self.logger = logging.getLogger(__name__)
|
2018-10-01 12:48:22 +00:00
|
|
|
|
|
|
|
if not level.lower() in LEVELS:
|
|
|
|
raise ValueError("Unknown log level '%s'" % level)
|
|
|
|
stdout_level = LEVELS[level.lower()]
|
|
|
|
|
|
|
|
self.logger.setLevel(LEVELS["trace"])
|
2018-08-05 10:30:43 +00:00
|
|
|
|
2018-08-05 11:13:25 +00:00
|
|
|
formatter = BitBotFormatter(
|
2018-09-18 23:43:44 +00:00
|
|
|
"%(asctime)s [%(levelname)s] %(message)s",
|
2018-09-21 09:35:05 +00:00
|
|
|
"%Y-%m-%dT%H:%M:%S.%fZ")
|
2018-08-05 10:30:43 +00:00
|
|
|
|
|
|
|
stdout_handler = logging.StreamHandler(sys.stdout)
|
2018-10-01 12:48:22 +00:00
|
|
|
stdout_handler.setLevel(stdout_level)
|
2018-08-05 10:30:43 +00:00
|
|
|
stdout_handler.setFormatter(formatter)
|
|
|
|
self.logger.addHandler(stdout_handler)
|
|
|
|
|
2018-11-26 11:09:43 +00:00
|
|
|
trace_handler = logging.handlers.TimedRotatingFileHandler(
|
|
|
|
os.path.join(location, "trace.log"), when="midnight", backupCount=5)
|
|
|
|
trace_handler.setLevel(LEVELS["trace"])
|
|
|
|
trace_handler.setFormatter(formatter)
|
|
|
|
self.logger.addHandler(trace_handler)
|
|
|
|
|
|
|
|
warn_handler = logging.FileHandler(os.path.join(location, "warn.log"))
|
|
|
|
warn_handler.setLevel(LEVELS["warn"])
|
|
|
|
warn_handler.setFormatter(formatter)
|
|
|
|
self.logger.addHandler(warn_handler)
|
2018-08-05 10:30:43 +00:00
|
|
|
|
2018-12-10 13:38:49 +00:00
|
|
|
self._queue = queue.Queue() # type: queue.Queue[typing.Tuple[str, typing.List, int, typing.Dict]]
|
2018-12-10 13:18:15 +00:00
|
|
|
self._thread = threading.Thread(target=self._loop)
|
|
|
|
self._thread.daemon = True
|
|
|
|
self._thread.start()
|
|
|
|
|
|
|
|
def _loop(self):
|
|
|
|
while True:
|
|
|
|
message, params, level, kwargs = self._queue.get(block=True)
|
|
|
|
self.logger.log(level, message, *params, **kwargs)
|
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def trace(self, message: str, params: typing.List, **kwargs):
|
2018-10-01 12:48:22 +00:00
|
|
|
self._log(message, params, LEVELS["trace"], kwargs)
|
2018-10-30 14:58:48 +00:00
|
|
|
def debug(self, message: str, params: typing.List, **kwargs):
|
2018-08-30 13:32:59 +00:00
|
|
|
self._log(message, params, logging.DEBUG, kwargs)
|
2018-10-30 14:58:48 +00:00
|
|
|
def info(self, message: str, params: typing.List, **kwargs):
|
2018-08-30 13:32:59 +00:00
|
|
|
self._log(message, params, logging.INFO, kwargs)
|
2018-10-30 14:58:48 +00:00
|
|
|
def warn(self, message: str, params: typing.List, **kwargs):
|
2018-08-30 13:32:59 +00:00
|
|
|
self._log(message, params, logging.WARN, kwargs)
|
2018-10-30 14:58:48 +00:00
|
|
|
def error(self, message: str, params: typing.List, **kwargs):
|
2018-08-30 13:32:59 +00:00
|
|
|
self._log(message, params, logging.ERROR, kwargs)
|
2018-10-30 14:58:48 +00:00
|
|
|
def critical(self, message: str, params: typing.List, **kwargs):
|
2018-08-30 13:32:59 +00:00
|
|
|
self._log(message, params, logging.CRITICAL, kwargs)
|
2018-10-30 14:58:48 +00:00
|
|
|
def _log(self, message: str, params: typing.List, level: int, kwargs: dict):
|
2018-12-12 11:33:29 +00:00
|
|
|
if kwargs.get("exc_info") == True:
|
|
|
|
# because we're doing the actual logging on another thread,
|
|
|
|
# we need to catch actual exception information here.
|
|
|
|
kwargs["exc_info"] = sys.exc_info()
|
2018-12-10 13:18:15 +00:00
|
|
|
self._queue.put((message, params, level, kwargs))
|