More messing with the unhandled / non-zero record code.
authorScott Gasch <[email protected]>
Sat, 10 Jun 2023 21:49:59 +0000 (14:49 -0700)
committerScott Gasch <[email protected]>
Sat, 10 Jun 2023 21:49:59 +0000 (14:49 -0700)
src/pyutils/bootstrap.py
src/pyutils/logging_utils.py

index 18cbc274a71083eb4526dccf18464147cb7e144c..a8dd5a11bcacc76382d0a9ca2834ed431c453f9b 100644 (file)
@@ -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)
index a4a5d2020364cc9b5296651d6a92d59de95a2425..79b1ba92c9d485a83f47a40f90ba26f839109e5a 100644 (file)
@@ -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: