Acquire log path from the environment

This commit is contained in:
R. Richard 2019-06-27 14:08:39 +02:00 committed by Kristóf Tóth
parent 149dcb0b4b
commit 778f155580
2 changed files with 11 additions and 12 deletions

View File

@ -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

View File

@ -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):