[Python-ideas] Adding exception logging function to syslog module.

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'):
               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)


 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