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 undercoveridiot
Recipients ellisj, mwh, tiagoaoa, undercoveridiot
Date 2009-08-03.21:59:31
SpamBayes Score 2.620919e-07
Marked as misclassified No
Message-id <1249336773.45.0.48918875378.issue1230540@psf.upfronthosting.co.za>
In-reply-to
Content
I found that the workaround suggested doesn't work when you have a
subclass of threading.Thread and you want to catch everything in the
module that contains the class to a common log.

Say you have a module with a socket server that spawns a thread on
accept and you want to log anything that tracebacks in the module. This
seems to work:

import sys
import logging
from functools import wraps

def myExceptHook(type, value, tb):
    """ Redirect tracebacks to error log """
    import traceback
    rawreport = traceback.format_exception(type, value, tb)
    report = '\n'.join(rawreport)
    log.error(report)

sys.excepthook = myExceptHook
    
def use_my_excepthook(view):
    """ Redirect any unexpected tracebacks """ 
    @wraps(view)
    def run(*args, **kwargs):
        try:
            return view(*args, **kwargs)
        except:
            sys.excepthook(*sys.exc_info())
    return run


Then in your thread subclass:


class MyThread(threading.Thread):
    def __init__(self, socket_conn):
        threading.Thread.__init__(self)
        self.my_conn = socket_conn

    @use_my_excepthook
    def run(self):
        """ Do stuff """
History
Date User Action Args
2009-08-03 21:59:33undercoveridiotsetrecipients: + undercoveridiot, mwh, ellisj, tiagoaoa
2009-08-03 21:59:33undercoveridiotsetmessageid: <1249336773.45.0.48918875378.issue1230540@psf.upfronthosting.co.za>
2009-08-03 21:59:32undercoveridiotlinkissue1230540 messages
2009-08-03 21:59:31undercoveridiotcreate