lozik
Databricks Partner

Hi @Retired_mod - `sys.excepthook` fails to trigger on an unhandled exception. Here's some example code using the logging module. This will write all logging messages to the logfile, except the the ones in `handleException`

 

import logging
import sys
import datetime
import os
logger = logging.getLogger("log_test")
def handleException(excType, excValue, traceback, logger=logger):
    logger.info("message from exception handler")
    logger.error("Uncaught exception", exc_info=(excType, excValue, traceback))
sys.excepthook = handleException

log_filepath = f'/dbfs/mnt/logs/log_test_{datetime.datetime.now().strftime("%Y%m%d%H%M%S")}.txt'
logging.basicConfig(
    format=f'%(asctime)s - %(name)s - %(levelname)s - %(funcName)s - %(lineno)d - %(message)s',
    level=logging.INFO,
    handlers=[
        logging.FileHandler(log_filepath),
        logging.StreamHandler(stream=sys.stdout)
    ]
)
logger.debug("this is an debug message")
logger.info("this is an info message")
logger.warning("this is an warning message")
logger.critical("this is an critical message")

raise RuntimeError("Test unhandled")