From 8bf18113b2c247f27e9fb1e304ace97fcc7d8e8d Mon Sep 17 00:00:00 2001 From: "R. Richard" Date: Tue, 18 Jun 2019 18:43:02 +0200 Subject: [PATCH] Implement custom logging logic --- lib/tfw/config/log.py | 116 ++++++++++++++++++++++++++ supervisor/components/tfw_server.conf | 2 +- supervisor/tfw_server.py | 6 +- 3 files changed, 119 insertions(+), 5 deletions(-) create mode 100644 lib/tfw/config/log.py diff --git a/lib/tfw/config/log.py b/lib/tfw/config/log.py new file mode 100644 index 0000000..332fef0 --- /dev/null +++ b/lib/tfw/config/log.py @@ -0,0 +1,116 @@ +# pylint: disable=bad-whitespace +from sys import stderr +from datetime import datetime +from logging import DEBUG, getLogger, Handler, Formatter, Filter + +TFW_LOG_PATH = '/var/log/tfw.log' + + +class COLOR: + BLACK = '\033[30m' + GREY = '\033[30;1m' + RED = '\033[31m' + BOLDRED = '\033[31;1m' + GREEN = '\033[32m' + BOLDGREEN = '\033[32;1m' + ORANGE = '\033[33m' + YELLOW = '\033[33;1m' + BLUE = '\033[34m' + BOLDBLUE = '\033[34;1m' + MAGENTA = '\033[35m' + BOLDMAGENTA = '\033[35;1m' + CYAN = '\033[36m' + BOLDCYAN = '\033[36;1m' + WHITE = '\033[37m' + BOLDWHITE = '\033[37;1m' + RESET = '\033[0m' + + +class TFWLog: + def __init__(self, path=TFW_LOG_PATH, level=DEBUG): + self.log = getLogger() + self.old_level = self.log.level + self.new_level = level + self.handler = TFWLogHandler(path) + self.handler.setFormatter(TFWLogFormatter()) + + def start(self): + self.log.setLevel(self.new_level) + self.log.addHandler(self.handler) + self.log.info('Logging started.') + + def stop(self): + self.log.info('Stop logging.') + self.log.setLevel(self.old_level) + self.handler.close() + self.log.removeHandler(self.handler) + + +class TFWLogHandler(Handler): + def __init__(self, path): + self.logfile = open(path, 'a+') + super().__init__() + + def emit(self, record): + short_entry, long_entry = self.format(record) + stderr.write(short_entry+'\n') + self.logfile.write(long_entry+'\n') + stderr.flush() + self.logfile.flush() + + def close(self): + self.logfile.close() + +class TFWLogFormatter(Formatter): + severity = { + 'CRITICAL' : COLOR.BOLDRED, + 'ERROR' : COLOR.RED, + 'WARNING' : COLOR.YELLOW, + 'INFO' : COLOR.BOLDGREEN, + 'DEBUG' : COLOR.BOLDWHITE, + 'NOTSET' : COLOR.CYAN + } + + def format(self, record): + date = datetime.utcfromtimestamp(record.created).strftime('%H:%M:%S') + if record.args: + tuple_args = (record.args,) if isinstance(record.args, dict) else record.args + clean_args = tuple((self.trim(arg) for arg in tuple_args)) + short_message = record.msg % clean_args + long_message = record.msg % record.args + else: + short_message = record.msg + long_message = record.msg + + short_entry = (f'[{COLOR.GREY}{date}{COLOR.RESET}|>' + f'{self.severity[record.levelname]}{record.module}:' + f'{record.levelname.lower()}{COLOR.RESET}] {short_message}') + long_entry = f'[{date}|>{record.module}:{record.levelname.lower()}] {long_message}' + return short_entry, long_entry + + def trim(self, value, in_dict=False): + if isinstance(value, dict): + trimmed = {k: self.trim(v, True) for k, v in value.items()} + return trimmed if in_dict else str(trimmed) + if isinstance(value, (int, float)): + return value + value_str = str(value) + return value_str if len(value_str) <= 20 else f'{value_str[:20]}...' + + +class TFWLogWhitelistFilter(Filter): + def __init__(self, names): + self.names = names + super().__init__() + + def filter(self, record): + return record.module in self.names + + +class TFWLogBlacklistFilter(Filter): + def __init__(self, names): + self.names = names + super().__init__() + + def filter(self, record): + return record.module not in self.names diff --git a/supervisor/components/tfw_server.conf b/supervisor/components/tfw_server.conf index f583be9..a131674 100644 --- a/supervisor/components/tfw_server.conf +++ b/supervisor/components/tfw_server.conf @@ -1,4 +1,4 @@ [program:tfwserver] user=root directory=%(ENV_TFW_SERVER_DIR)s -command=python3 tfw_server.py +command=python3 -u tfw_server.py diff --git a/supervisor/tfw_server.py b/supervisor/tfw_server.py index 0d52bbf..35fc2fa 100644 --- a/supervisor/tfw_server.py +++ b/supervisor/tfw_server.py @@ -1,12 +1,10 @@ -import logging - from tornado.ioloop import IOLoop +from tfw.config.log import TFWLog from tfw.server import TFWServer -logging.basicConfig(level=logging.DEBUG) - if __name__ == '__main__': + TFWLog().start() TFWServer().listen() IOLoop.instance().start()