<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Python callback functions fail to trigger in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/python-callback-functions-fail-to-trigger/m-p/81000#M36192</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/9"&gt;@Retired_mod&lt;/a&gt;&amp;nbsp;- `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`&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 29 Jul 2024 17:02:18 GMT</pubDate>
    <dc:creator>lozik</dc:creator>
    <dc:date>2024-07-29T17:02:18Z</dc:date>
    <item>
      <title>Python callback functions fail to trigger</title>
      <link>https://community.databricks.com/t5/data-engineering/python-callback-functions-fail-to-trigger/m-p/79992#M35892</link>
      <description>&lt;P&gt;How can I get sys.exceptionhook and atexit module to trigger a callback function on exit of a python notebook? These fail to work when an unhandled exception is encountered (exceptionhook), or the program exits (atexit).&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2024 19:21:19 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/python-callback-functions-fail-to-trigger/m-p/79992#M35892</guid>
      <dc:creator>lozik</dc:creator>
      <dc:date>2024-07-22T19:21:19Z</dc:date>
    </item>
    <item>
      <title>Re: Python callback functions fail to trigger</title>
      <link>https://community.databricks.com/t5/data-engineering/python-callback-functions-fail-to-trigger/m-p/81000#M36192</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/9"&gt;@Retired_mod&lt;/a&gt;&amp;nbsp;- `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`&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jul 2024 17:02:18 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/python-callback-functions-fail-to-trigger/m-p/81000#M36192</guid>
      <dc:creator>lozik</dc:creator>
      <dc:date>2024-07-29T17:02:18Z</dc:date>
    </item>
    <item>
      <title>Re: Python callback functions fail to trigger</title>
      <link>https://community.databricks.com/t5/data-engineering/python-callback-functions-fail-to-trigger/m-p/110398#M43561</link>
      <description>&lt;P&gt;Hey Lozik,&lt;/P&gt;&lt;P&gt;Ran into this myself as well. The reason this doesn't work is because Databricks is using Ipython under the hood.&lt;/P&gt;&lt;P&gt;The following codesnippet creates an exception hook for all exceptions (using the general Exception), it's also possible to specify which exception you wish to add an hook. `&lt;SPAN&gt;exception_handler&lt;/SPAN&gt;` was the function I used for handling the exceptions&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Add hook to exception handling
ipy = get_ipython()
ipy.set_custom_exc((Exception,), exception_handler)&lt;/LI-CODE&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;Additional documentation can be found here: &lt;A href="https://pydocs.github.io/p/IPython/8.4.0/api/IPython.core.interactiveshell.InteractiveShell.set_custom_exc.html" target="_blank" rel="noopener"&gt;Document&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Good luck!&lt;/P&gt;</description>
      <pubDate>Mon, 17 Feb 2025 13:22:28 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/python-callback-functions-fail-to-trigger/m-p/110398#M43561</guid>
      <dc:creator>Pieter</dc:creator>
      <dc:date>2025-02-17T13:22:28Z</dc:date>
    </item>
  </channel>
</rss>

