From 778f15558079619818f26e4cdaa742b8f3efb482 Mon Sep 17 00:00:00 2001 From: "R. Richard" Date: Thu, 27 Jun 2019 14:08:39 +0200 Subject: [PATCH] Acquire log path from the environment --- Dockerfile | 1 + lib/tfw/config/log.py | 22 ++++++++++------------ 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/Dockerfile b/Dockerfile index 94eee57..7bfe6e8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -42,6 +42,7 @@ ENV PYTHONPATH="/usr/local/lib" \ TFW_SNAPSHOTS_DIR="/.tfw/snapshots" \ TFW_AUTH_KEY="/tmp/tfw-auth.key" \ TFW_HISTFILE="/home/${AVATAO_USER}/.bash_history" \ + TFW_LOGFILE="/var/log/tfw.log" \ PROMPT_COMMAND="history -a" COPY bashrc /tmp diff --git a/lib/tfw/config/log.py b/lib/tfw/config/log.py index 827bff9..6c85aa7 100644 --- a/lib/tfw/config/log.py +++ b/lib/tfw/config/log.py @@ -5,7 +5,7 @@ from datetime import datetime from traceback import format_exception, walk_tb from logging import DEBUG, getLogger, Handler, Formatter, Filter -TFW_LOG_PATH = '/var/log/tfw.log' +from .envvars import TFWENV class COLOR: @@ -29,20 +29,18 @@ class COLOR: class TFWLog: - def __init__(self, path=TFW_LOG_PATH, level=DEBUG): + def __init__(self, path=TFWENV.LOGFILE, level=DEBUG): self.log = getLogger() self.old_level = self.log.level self.new_level = level self.handler = TFWLogHandler(path) - self.handler.setFormatter(TFWLogFormatter()) + self.handler.setFormatter(TFWLogFormatter(20)) 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) @@ -64,7 +62,7 @@ class TFWLogHandler(Handler): self.logfile.close() class TFWLogFormatter(Formatter): - severity = { + severity_to_color = { 'CRITICAL' : COLOR.BOLDRED, 'ERROR' : COLOR.RED, 'WARNING' : COLOR.YELLOW, @@ -73,7 +71,8 @@ class TFWLogFormatter(Formatter): 'NOTSET' : COLOR.CYAN } - def __init__(self): + def __init__(self, limit): + self.limit = limit self.last_trace = None super().__init__() @@ -100,21 +99,20 @@ class TFWLogFormatter(Formatter): trace = '' short_entry = (f'[{COLOR.GREY}{date}{COLOR.RESET}|>' - f'{self.severity[record.levelname]}{record.module}:' + f'{self.severity_to_color[record.levelname]}{record.module}:' f'{record.levelname.lower()}{COLOR.RESET}] {short_message}' f'{trace}') long_entry = (f'[{date}|>{record.module}:{record.levelname.lower()}] ' f'{long_message}{trace}') return short_entry, long_entry - def trim(self, value, in_dict=False): + def trim(self, value): if isinstance(value, dict): - trimmed = {k: self.trim(v, True) for k, v in value.items()} - return trimmed if in_dict else str(trimmed) + return {k: self.trim(v, True) for k, v in value.items()} if isinstance(value, (int, float)): return value value_str = str(value) - return value_str if len(value_str) <= 20 else f'{value_str[:20]}...' + return value_str if len(value_str) <= self.limit else f'{value_str[:self.limit]}...' @staticmethod def fetch_exception_origin(trace):