mirror of
https://github.com/archlinux-jerry/buildbot
synced 2024-11-22 04:50:41 +08:00
utils: rewrite configure_logger
This commit is contained in:
parent
fa5d4a20ab
commit
4089802b00
1 changed files with 20 additions and 30 deletions
50
utils.py
50
utils.py
|
@ -219,7 +219,7 @@ def get_arch_from_pkgbuild(fpath):
|
||||||
raise TypeError('Unexpected PKGBUILD')
|
raise TypeError('Unexpected PKGBUILD')
|
||||||
|
|
||||||
def print_exc_plus():
|
def print_exc_plus():
|
||||||
logger.log(49, format_exc_plus())
|
logger.critical("Exception caught.\nPrinting stack traceback\n" + format_exc_plus())
|
||||||
|
|
||||||
def format_exc_plus():
|
def format_exc_plus():
|
||||||
"""
|
"""
|
||||||
|
@ -262,34 +262,13 @@ def format_exc_plus():
|
||||||
def configure_logger(logger, format='%(asctime)s - %(name)-18s - %(levelname)s - %(message)s',
|
def configure_logger(logger, format='%(asctime)s - %(name)-18s - %(levelname)s - %(message)s',
|
||||||
level=logging.INFO, logfile=None, flevel=logging.DEBUG, rotate_size=None,
|
level=logging.INFO, logfile=None, flevel=logging.DEBUG, rotate_size=None,
|
||||||
enable_notify=False, consolelog=None):
|
enable_notify=False, consolelog=None):
|
||||||
def __send(*args):
|
|
||||||
pass
|
|
||||||
if enable_notify:
|
|
||||||
try:
|
|
||||||
from notify import send
|
|
||||||
except ModuleNotFoundError:
|
|
||||||
send = __send
|
|
||||||
else:
|
|
||||||
send = __send
|
|
||||||
|
|
||||||
class ExceptionFormatter(logging.Formatter):
|
class NotifyHandler(logging.NullHandler):
|
||||||
def __init__(self, *args, notify=False, **kwargs):
|
def handle(self, record):
|
||||||
self.__notify = notify
|
send(self.formatter.format(record))
|
||||||
self.__lastnt = ""
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
def format(self, record):
|
|
||||||
if record.levelno == 49:
|
|
||||||
record.msg = 'Exception caught.\nPrinting stack traceback\n' + record.msg
|
|
||||||
fmtr = super().format(record)
|
|
||||||
if self.__notify and not (self.__lastnt == fmtr):
|
|
||||||
self.lastnt = fmtr
|
|
||||||
send(fmtr)
|
|
||||||
return fmtr
|
|
||||||
|
|
||||||
logger.setLevel(logging.DEBUG)
|
logger.setLevel(logging.DEBUG)
|
||||||
fformatter = ExceptionFormatter(fmt=format, notify=False)
|
formatter = logging.Formatter(fmt=format)
|
||||||
cformatter = ExceptionFormatter(fmt=format, notify=enable_notify)
|
|
||||||
logging.addLevelName(49, 'Exception')
|
|
||||||
# create file handler
|
# create file handler
|
||||||
if logfile:
|
if logfile:
|
||||||
assert type(logfile) is str
|
assert type(logfile) is str
|
||||||
|
@ -299,17 +278,28 @@ def configure_logger(logger, format='%(asctime)s - %(name)-18s - %(levelname)s -
|
||||||
else:
|
else:
|
||||||
fh = logging.FileHandler(logfile)
|
fh = logging.FileHandler(logfile)
|
||||||
fh.setLevel(flevel)
|
fh.setLevel(flevel)
|
||||||
fh.setFormatter(fformatter)
|
fh.setFormatter(formatter)
|
||||||
logger.addHandler(fh)
|
logger.addHandler(fh)
|
||||||
# create console handler
|
# create console handler
|
||||||
ch = logging.StreamHandler()
|
ch = logging.StreamHandler()
|
||||||
ch.setLevel(level)
|
ch.setLevel(level)
|
||||||
ch.setFormatter(cformatter)
|
ch.setFormatter(formatter)
|
||||||
logger.addHandler(ch)
|
logger.addHandler(ch)
|
||||||
|
# for client.printlog
|
||||||
if consolelog:
|
if consolelog:
|
||||||
assert type(consolelog) is str
|
assert type(consolelog) is str
|
||||||
cfh = logging.FileHandler(consolelog)
|
cfh = logging.FileHandler(consolelog)
|
||||||
cfh.setLevel(level)
|
cfh.setLevel(level)
|
||||||
cfh.setFormatter(cformatter)
|
cfh.setFormatter(formatter)
|
||||||
logger.addHandler(cfh)
|
logger.addHandler(cfh)
|
||||||
# for client.printlog
|
# notify
|
||||||
|
if enable_notify:
|
||||||
|
try:
|
||||||
|
from notify import send
|
||||||
|
except ModuleNotFoundError:
|
||||||
|
print('Failed to import notify.send')
|
||||||
|
else:
|
||||||
|
nh = NotifyHandler()
|
||||||
|
nh.setLevel(level)
|
||||||
|
nh.setFormatter(formatter)
|
||||||
|
logger.addHandler(nh)
|
||||||
|
|
Loading…
Reference in a new issue