This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author elias
Recipients elias
Date 2011-03-28.22:29:13
SpamBayes Score 0.0
Marked as misclassified No
Message-id <1301351356.06.0.0947935400826.issue11705@psf.upfronthosting.co.za>
In-reply-to
Content
I am trying to design a Python program that logs all uncaught exceptions using the logging module. I am doing this by using the sys.excepthook function to override the default exception handling. I noticed that if I run the program directly from the command line, it works fine, but if I try to import the file, it doesn't work. It seems like the sys.excepthook function is unaware of the logging module. Here is an example:

    #! /usr/bin/env python2.7
    import logging, sys
    
    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)
    logger.addHandler(logging.FileHandler("test.log"))
    
    print "outside of exception handler: logger = %s" % logger
    
    def handleException(excType, excValue, traceback):
    	#global logger	# this function doesn't work whether or not I include this line
    	print "inside exception handler: logger = %s" % logger
    	logger.error("Uncaught exception", exc_info=(excType, excValue, traceback))
    
    sys.excepthook = handleException
    
    logger.debug("starting")
    asdf	# create an exception

If I run this from the command line (`./loggingTest.py`), it works fine. The exception gets logged, and I see this output:

    outside of exception handler: logger = <logging.RootLogger object at 0x7f2022eab950>
    inside exception handler: logger = <logging.RootLogger object at 0x7f2022eab950>

However, if I run the Python interpreter and try to import the file (`import loggingTest`), it acts strangely. The exception doesn't get logged and I see this:

    outside of exception handler: logger = <logging.RootLogger object at 0x7f8ab04f3ad0>
    inside exception handler: logger = None
    Error in sys.excepthook:
    Traceback (most recent call last):
      File "loggingTest.py", line 13, in handleException
        logger.error("Uncaught exception", exc_info=(excType, excValue, traceback))
    AttributeError: 'NoneType' object has no attribute 'error'
    
    Original exception was:
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "loggingTest.py", line 18, in <module>
        asdf	# create an exception
    NameError: name 'asdf' is not defined

I can maybe work around this problem by importing the logging module again within sys.excepthook, but I am still curious: why is this happening?

The above text was copied from my question on Stack Overflow (http://stackoverflow.com/questions/5451746/sys-excepthook-doesnt-work-in-imported-modules). Someone on there (Jochen Ritzel) has mentioned that he noticed this bug in Python 2.7.1 but not 2.7. To me, it seems like the sys.excepthook function is unaware of any imported modules, unless those modules are imported from inside the function.
History
Date User Action Args
2011-03-28 22:29:16eliassetrecipients: + elias
2011-03-28 22:29:16eliassetmessageid: <1301351356.06.0.0947935400826.issue11705@psf.upfronthosting.co.za>
2011-03-28 22:29:14eliaslinkissue11705 messages
2011-03-28 22:29:13eliascreate