[Python-ideas] Adding exception logging function to syslog module. (original) (raw)

Sean Reifschneider jafo at tummy.com
Thu Mar 18 13:08:47 CET 2010


As someone who writes mostly programs that run unattended on servers, one thing I often want to do is have my tracebacks logged to syslog. I would propose adding something like the following to the syslog module:

def logexceptions(also_stderr = True): class ExceptHook: def init(self, useStderr = False): self.useStderr = useStderr

     def __call__(self, etype, evalue, etb):
        import traceback, string
        tb = traceback.format_exception(*(etype, evalue, etb))
        tb = map(string.rstrip, tb)
        tb = string.join(tb, '\n')
        for line in string.split(tb, '\n'):
           syslog.syslog(line)
           if self.useStderr:
              sys.stderr.write(line + '\n')
  sys.excepthook = ExceptHook(also_stderr)

Now, the downside to this is that currently the syslog module is entirely in C. So either I'd need to implement the above in C, or I'd need to add a Python wrapper to the C module and use that.

This would allow users to also log exceptions to syslog by doing:

import syslog syslog.openlog('myprogram', syslog.LOG_PID, syslog.LOG_MAIL) syslog.logexceptions()

Thoughts?

Thanks, Sean

Python: even though everyone's using it now, somehow it's still the coolest. Sean Reifschneider, Member of Technical Staff <jafo at tummy.com> tummy.com, ltd. - Linux Consulting since 1995: Ask me about High Availability



More information about the Python-ideas mailing list