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 eric.smith
Recipients eric.smith, jafo, vstinner
Date 2010-03-25.09:56:06
SpamBayes Score 6.266188e-06
Marked as misclassified No
Message-id <1269510969.14.0.490821612334.issue8214@psf.upfronthosting.co.za>
In-reply-to
Content
Here's a version that would be more analogous to what the C implementation would look like. It uses a class instead of a closure to capture the "chain" value. The 2 exposed functions syslog_exception and enable_exception_logging are the new APIs in this proposal, which would be written in C as part of the syslog module. The class is a hidden implementation detail.

########################
import traceback
import syslog
import sys

def syslog_exception(etype, evalue, etb):
    # The result of traceback.format_exception might contain
    # embedded newlines, so we have the nested loops.
    for line in traceback.format_exception(etype, evalue, etb):
        for line in line.rstrip().split('\n'):
            syslog.syslog(line)

class _Hook(object):
    def __init__(self, chain):
        # Should we chain to the existing sys.excepthook?
        self._current_hook = sys.excepthook if chain else None

    def _hook(self, etype, evalue, etb):
        if self._current_hook:
            self._current_hook(etype, evalue, etb)
        syslog_exception(etype, evalue, etb)

def enable_exception_logging(chain=True):
    sys.excepthook = _Hook(chain)._hook
History
Date User Action Args
2010-03-25 09:56:09eric.smithsetrecipients: + eric.smith, jafo, vstinner
2010-03-25 09:56:09eric.smithsetmessageid: <1269510969.14.0.490821612334.issue8214@psf.upfronthosting.co.za>
2010-03-25 09:56:06eric.smithlinkissue8214 messages
2010-03-25 09:56:06eric.smithcreate