Remove option log_exception, always log it under our logger.

This unifies the thought process so that everything is done via the
log lines, not these separate config variables. Caller can mute this
module if they want to. Also, it was previously appending directly
to the message but now we can have our logger's name on it so it's
more clear who's reporting what.
This commit is contained in:
voussoir 2021-07-04 15:42:57 -07:00
parent 2971d7e4d6
commit f18f0fd2df
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB

View file

@ -134,32 +134,30 @@ class LogHandlerContext:
of this to notify. This saves you from having to call handler.notify
yourself, because it will occur when the context ends.
'''
def __init__(self, log, handler, log_exception=True):
def __init__(self, log, handler):
'''
log:
Your logger from logging.getLogger
handler:
Your operatornotify.LogHandler
log_exception:
If the context is killed by an exception, include the traceback at
the bottom of the notification.
'''
self.log = log
self.handler = handler
self.log_exception = log_exception
def __enter__(self):
self.log.addHandler(self.handler)
return self
def __exit__(self, exc_type, exc_value, exc_traceback):
if self.log_exception and exc_type not in (None, KeyboardInterrupt):
if exc_type not in (None, KeyboardInterrupt):
exc_text = traceback.format_exception(exc_type, exc_value, exc_traceback)
exc_text = ''.join(exc_text)
exc_text = f'\n\n{exc_text}\n'
self.handler.log_buffer.write(exc_text)
exc_text = f'\n{exc_text}\n'
# Intentionally using module's log, not self.log because I think
# it should be clear who emitted the message, and the caller can
# mute this module if they want to.
log.error(exc_text)
self.handler.notify()
self.log.removeHandler(self.handler)
@ -224,7 +222,7 @@ def main_log_context(argv, subject, *args, **kwargs):
handler = LogHandler(subject, *args, **kwargs)
handler.setLevel(level)
handler.setFormatter(vlogging.Formatter('{levelname}:{name}:{message}', style='{'))
context = LogHandlerContext(log, handler, log_exception=True)
context = LogHandlerContext(log, handler)
return (context, argv)
def operatornotify_argparse(args):