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.62092e-07
Marked as misclassified No
Message-id <>
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)

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

Then in your thread subclass:

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

    def run(self):
        """ Do stuff """
Date User Action Args
2009-08-03 21:59:33undercoveridiotsetrecipients: + undercoveridiot, mwh, ellisj, tiagoaoa
2009-08-03 21:59:33undercoveridiotsetmessageid: <>
2009-08-03 21:59:32undercoveridiotlinkissue1230540 messages
2009-08-03 21:59:31undercoveridiotcreate