From: Scott Gasch Date: Sat, 10 Jun 2023 21:49:59 +0000 (-0700) Subject: More messing with the unhandled / non-zero record code. X-Git-Url: https://wannabe.guru.org/gitweb/?a=commitdiff_plain;h=ea0e772e9b27f53df931a99c7c36958688bbe4ad;p=pyutils.git More messing with the unhandled / non-zero record code. --- diff --git a/src/pyutils/bootstrap.py b/src/pyutils/bootstrap.py index 18cbc27..a8dd5a1 100644 --- a/src/pyutils/bootstrap.py +++ b/src/pyutils/bootstrap.py @@ -118,10 +118,7 @@ def handle_uncaught_exception(exc_type, exc_value, exc_tb): msg = f"Unhandled top level exception {exc_type}" logger.exception(msg) print(msg, file=sys.stderr) - try: - logging_utils.unhandled_top_level_exception(exc_type, exc_value, exc_tb) - except Exception: - pass + logging_utils.unhandled_top_level_exception(exc_type, exc_value, exc_tb) if issubclass(exc_type, KeyboardInterrupt): sys.__excepthook__(exc_type, exc_value, exc_tb) return @@ -434,8 +431,8 @@ def initialize(entry_point): # If it doesn't return cleanly, call attention to the return value. base_filename = os.path.basename(entry_filename) if ret is not None and ret != 0: - logger.error("%s: Exit %s", base_filename, ret) - logging_utils.non_zero_return_value(ret) + if not logging_utils.non_zero_return_value(ret): + logger.error("%s: Exit %s", base_filename, ret) else: logger.debug("%s: Exit %s", base_filename, ret) sys.exit(ret) diff --git a/src/pyutils/logging_utils.py b/src/pyutils/logging_utils.py index a4a5d20..79b1ba9 100644 --- a/src/pyutils/logging_utils.py +++ b/src/pyutils/logging_utils.py @@ -653,6 +653,16 @@ class LoggingContext(contextlib.ContextDecorator): But inside, the decorator has changed us to DEBUG >>> logger.debug("And, of course, out here we're still at INFO afterwards") + Args: + logger: the logger on which to operate + level: the new level to set for the duration of the context + handlers: additional handlers to add for the duration of the context + prefix: the prefix string to set for the duration of the context + suffix: the suffix string to set for the duration of the context + + Returns: + The modified logger. + """ self.logger = logger self.level = level @@ -1215,7 +1225,7 @@ def _get_systemwide_abnormal_exit_handler(filename: str) -> FileHandler: return handler -def non_zero_return_value(ret: Any): +def non_zero_return_value(ret: Any) -> bool: """ Special method hooked from bootstrap.py to optionally keep a system-wide record of non-zero python program exits. @@ -1230,13 +1240,15 @@ def non_zero_return_value(ret: Any): handler = _get_systemwide_abnormal_exit_handler(record) program = config.PROGRAM_NAME args = config.ORIG_ARGV - with LoggingContext(logger, handlers=[handler], level=logging.INFO): - logger.info('%s (%s) exited with non-zero value %s', program, args, ret) + with LoggingContext(logger, handlers=[handler], level=logging.ERROR) as log: + log.error('%s (%s) Exit %s', program, args, ret) + return True except Exception: pass + return False -def unhandled_top_level_exception(exc_type: type, exc_value, exc_tb): +def unhandled_top_level_exception(exc_type: type, exc_value, exc_tb) -> bool: """ Special method hooked from bootstrap.py to optionally keep a system-wide record of unhandled top level exceptions. @@ -1253,9 +1265,9 @@ def unhandled_top_level_exception(exc_type: type, exc_value, exc_tb): args = config.ORIG_ARGV site_file = exc_tb.tb_frame.f_code.co_filename site_lineno = exc_tb.tb_lineno - with LoggingContext(logger, handlers=[handler], level=logging.INFO): - logger.info( - '%s (%s) took an unhandled top-level exception (type=%s(%s)) at %s:%s', + with LoggingContext(logger, handlers=[handler], level=logging.ERROR) as log: + log.error( + '%s (%s) unhandled top-level exception (type=%s(%s)) at %s:%s', program, args, exc_type.__name__, @@ -1263,8 +1275,10 @@ def unhandled_top_level_exception(exc_type: type, exc_value, exc_tb): site_file, site_lineno, ) + return True except Exception: pass + return False def hlog(message: str) -> None: