mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-22 02:51:33 +00:00
Refactor TFWLog
This commit is contained in:
parent
bcee486463
commit
28c3e68b5a
@ -1,29 +1,18 @@
|
||||
# pylint: disable=bad-whitespace
|
||||
from sys import stderr
|
||||
from collections import deque
|
||||
from datetime import datetime
|
||||
from traceback import format_exception, walk_tb
|
||||
from logging import DEBUG, getLogger, Handler, Formatter, Filter
|
||||
|
||||
from .envvars import TFWENV
|
||||
|
||||
|
||||
class COLOR:
|
||||
BLACK = '\033[30m'
|
||||
class Color:
|
||||
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'
|
||||
|
||||
@ -63,17 +52,16 @@ class TFWLogHandler(Handler):
|
||||
|
||||
class TFWLogFormatter(Formatter):
|
||||
severity_to_color = {
|
||||
'CRITICAL' : COLOR.BOLDRED,
|
||||
'ERROR' : COLOR.RED,
|
||||
'WARNING' : COLOR.YELLOW,
|
||||
'INFO' : COLOR.BOLDGREEN,
|
||||
'DEBUG' : COLOR.BOLDWHITE,
|
||||
'NOTSET' : COLOR.CYAN
|
||||
'CRITICAL' : Color.BOLDRED,
|
||||
'ERROR' : Color.RED,
|
||||
'WARNING' : Color.YELLOW,
|
||||
'INFO' : Color.BOLDGREEN,
|
||||
'DEBUG' : Color.BOLDWHITE,
|
||||
'NOTSET' : Color.CYAN
|
||||
}
|
||||
|
||||
def __init__(self, limit):
|
||||
self.limit = limit
|
||||
self.last_trace = None
|
||||
super().__init__()
|
||||
|
||||
def format(self, record):
|
||||
@ -87,23 +75,11 @@ class TFWLogFormatter(Formatter):
|
||||
short_message = record.msg
|
||||
long_message = record.msg
|
||||
|
||||
if record.exc_info:
|
||||
current_trace = self.fetch_exception_origin(record.exc_info[2])
|
||||
if current_trace != self.last_trace:
|
||||
self.last_trace = current_trace
|
||||
trace = '\n'+''.join(format_exception(*record.exc_info))
|
||||
else:
|
||||
trace = (f'\nSee previous traceback...\n'
|
||||
f'{record.exc_info[0].__name__}: {record.exc_info[1]}')
|
||||
else:
|
||||
trace = ''
|
||||
|
||||
short_entry = (f'[{COLOR.GREY}{date}{COLOR.RESET}|>'
|
||||
short_entry = (f'[{Color.GREY}{date}{Color.RESET}|>'
|
||||
f'{self.severity_to_color[record.levelname]}{record.module}:'
|
||||
f'{record.levelname.lower()}{COLOR.RESET}] {short_message}'
|
||||
f'{trace}')
|
||||
f'{record.levelname.lower()}{Color.RESET}] {short_message}')
|
||||
long_entry = (f'[{date}|>{record.module}:{record.levelname.lower()}] '
|
||||
f'{long_message}{trace}')
|
||||
f'{long_message}')
|
||||
return short_entry, long_entry
|
||||
|
||||
def trim(self, value):
|
||||
@ -114,12 +90,8 @@ class TFWLogFormatter(Formatter):
|
||||
return value_str if len(value_str) <= self.limit else f'{value_str[:self.limit]}...'
|
||||
return value
|
||||
|
||||
@staticmethod
|
||||
def fetch_exception_origin(trace):
|
||||
return deque(walk_tb(trace), maxlen=1).pop()
|
||||
|
||||
|
||||
class TFWLogWhitelistFilter(Filter):
|
||||
class WhitelistFilter(Filter):
|
||||
def __init__(self, names):
|
||||
self.names = names
|
||||
super().__init__()
|
||||
@ -128,7 +100,7 @@ class TFWLogWhitelistFilter(Filter):
|
||||
return record.module in self.names
|
||||
|
||||
|
||||
class TFWLogBlacklistFilter(Filter):
|
||||
class BlacklistFilter(Filter):
|
||||
def __init__(self, names):
|
||||
self.names = names
|
||||
super().__init__()
|
||||
|
Loading…
Reference in New Issue
Block a user