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()