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