From f18f0fd2df50f5718ea00def4416f50c01b6cfe4 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sun, 4 Jul 2021 15:42:57 -0700 Subject: [PATCH] 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. --- voussoirkit/operatornotify.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/voussoirkit/operatornotify.py b/voussoirkit/operatornotify.py index 4ad1a20..4cda2e5 100644 --- a/voussoirkit/operatornotify.py +++ b/voussoirkit/operatornotify.py @@ -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):