2019-02-17 14:15:40 +00:00
|
|
|
import datetime, logging, logging.handlers, os, queue, sys, time, typing
|
|
|
|
from src import utils
|
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):
|
|
|
|
def formatTime(self, record, datefmt=None):
|
2019-02-17 14:15:40 +00:00
|
|
|
datetime_obj = datetime.datetime.fromtimestamp(record.created)
|
2019-04-23 20:27:43 +00:00
|
|
|
return utils.iso8601_format(datetime_obj, milliseconds=True)
|
2018-08-05 10:30:43 +00:00
|
|
|
|
2019-10-11 14:12:26 +00:00
|
|
|
class HookedHandler(logging.StreamHandler):
|
|
|
|
def __init__(self, func: typing.Callable[[int, str], None]):
|
|
|
|
logging.StreamHandler.__init__(self)
|
|
|
|
self._func = func
|
|
|
|
|
|
|
|
def emit(self, record):
|
|
|
|
self._func(record.levelno, self.format(record))
|
|
|
|
|
2018-08-28 11:23:57 +00:00
|
|
|
class Log(object):
|
2019-03-04 19:35:18 +00:00
|
|
|
def __init__(self, to_file: bool, level: str, location: str):
|
2019-10-11 14:12:26 +00:00
|
|
|
self._hooks = []
|
|
|
|
|
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
|
|
|
|
2019-08-30 12:46:21 +00:00
|
|
|
formatter = BitBotFormatter("%(asctime)s [%(levelname)s] %(message)s")
|
2018-08-05 10:30:43 +00:00
|
|
|
|
|
|
|
stdout_handler = logging.StreamHandler(sys.stdout)
|
2019-10-09 16:54:12 +00:00
|
|
|
stdout_handler.setLevel(stdout_level)
|
2018-08-05 10:30:43 +00:00
|
|
|
stdout_handler.setFormatter(formatter)
|
|
|
|
self.logger.addHandler(stdout_handler)
|
|
|
|
|
2019-10-11 16:17:43 +00:00
|
|
|
hook_handler = HookedHandler(self._on_log)
|
|
|
|
hook_handler.setLevel(LEVELS["debug"])
|
|
|
|
hook_handler.setFormatter(formatter)
|
|
|
|
self.logger.addHandler(hook_handler)
|
2019-10-11 14:12:26 +00:00
|
|
|
|
2019-03-04 19:35:18 +00:00
|
|
|
if to_file:
|
|
|
|
trace_path = os.path.join(location, "trace.log")
|
|
|
|
trace_handler = logging.handlers.TimedRotatingFileHandler(
|
|
|
|
trace_path, when="midnight", backupCount=5)
|
|
|
|
trace_handler.setLevel(LEVELS["trace"])
|
|
|
|
trace_handler.setFormatter(formatter)
|
|
|
|
self.logger.addHandler(trace_handler)
|
2018-11-26 11:09:43 +00:00
|
|
|
|
2019-10-09 16:36:17 +00:00
|
|
|
info_path = os.path.join(location, "info.log")
|
|
|
|
info_handler = logging.handlers.TimedRotatingFileHandler(
|
|
|
|
info_path, when="midnight", backupCount=0)
|
|
|
|
info_handler.setLevel(LEVELS["info"])
|
|
|
|
info_handler.setFormatter(formatter)
|
|
|
|
self.logger.addHandler(info_handler)
|
|
|
|
|
2019-03-04 19:35:18 +00:00
|
|
|
warn_path = os.path.join(location, "warn.log")
|
|
|
|
warn_handler = logging.FileHandler(warn_path)
|
|
|
|
warn_handler.setLevel(LEVELS["warn"])
|
|
|
|
warn_handler.setFormatter(formatter)
|
|
|
|
self.logger.addHandler(warn_handler)
|
2018-08-05 10:30:43 +00:00
|
|
|
|
2019-10-11 14:12:26 +00:00
|
|
|
def hook(self, func: typing.Callable[[int, str], None]):
|
|
|
|
self._hooks.append(func)
|
|
|
|
def _on_log(self, levelno, line):
|
|
|
|
for func in self._hooks:
|
|
|
|
func(levelno, line)
|
|
|
|
|
2019-06-06 11:59:47 +00:00
|
|
|
def trace(self, message: str, params: typing.List=None, **kwargs):
|
2018-10-01 12:48:22 +00:00
|
|
|
self._log(message, params, LEVELS["trace"], kwargs)
|
2019-06-06 11:59:47 +00:00
|
|
|
def debug(self, message: str, params: typing.List=None, **kwargs):
|
2018-08-30 13:32:59 +00:00
|
|
|
self._log(message, params, logging.DEBUG, kwargs)
|
2019-06-06 11:59:47 +00:00
|
|
|
def info(self, message: str, params: typing.List=None, **kwargs):
|
2018-08-30 13:32:59 +00:00
|
|
|
self._log(message, params, logging.INFO, kwargs)
|
2019-06-06 11:59:47 +00:00
|
|
|
def warn(self, message: str, params: typing.List=None, **kwargs):
|
2018-08-30 13:32:59 +00:00
|
|
|
self._log(message, params, logging.WARN, kwargs)
|
2019-06-06 11:59:47 +00:00
|
|
|
def error(self, message: str, params: typing.List=None, **kwargs):
|
2018-08-30 13:32:59 +00:00
|
|
|
self._log(message, params, logging.ERROR, kwargs)
|
2019-06-06 11:59:47 +00:00
|
|
|
def critical(self, message: str, params: typing.List=None, **kwargs):
|
2018-08-30 13:32:59 +00:00
|
|
|
self._log(message, params, logging.CRITICAL, kwargs)
|
2019-06-07 12:35:20 +00:00
|
|
|
def _log(self, message: str, params: typing.Optional[typing.List],
|
|
|
|
level: int, kwargs: dict):
|
2019-06-06 11:59:47 +00:00
|
|
|
self.logger.log(level, message, *(params or []), **kwargs)
|