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
# 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)
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
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.
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.
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__,
site_file,
site_lineno,
)
+ return True
except Exception:
pass
+ return False
def hlog(message: str) -> None: