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,
Python: even though everyone's using it now, somehow it's still the coolest. Sean Reifschneider, Member of Technical Staff jafo@tummy.com tummy.com, ltd. - Linux Consulting since 1995: Ask me about High Availability
Sean Reifschneider wrote:
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?
I think this would be a great addition. I have to do similar things all of the time, although I never though of using sys.excepthook, for some reason. I'll have to steal your code in the meantime!
A simpler pure Python version would be:
from __future__ import print_function
def logexceptions(also_stderr=True): import sys import traceback import syslog 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)
if also_stderr:
print(line, file=sys.stderr)
sys.excepthook = syslog_exception
logexceptions(True)
If you need any help implementing this in C, I'll assist (although I'm out of town for 2 weeks starting Thursday).
-- Eric.
Eric Smith wrote:
I think this would be a great addition. I have to do similar things all of the time, although I never though of using sys.excepthook, for some reason. I'll have to steal your code in the meantime!
A simpler pure Python version would be:
from __future__ import print_function
def logexceptions(also_stderr=True): import sys import traceback import syslog 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)
if also_stderr:
print(line, file=sys.stderr)
sys.excepthook = syslog_exception
logexceptions(True)
On second thought, maybe it would be better to optionally chain to the existing sys.excepthook instead of assuming it writes to stderr. The above code would become:
def logexceptions(chain=True): import sys import traceback import syslog current_hook = sys.excepthook def syslog_exception(etype, evalue, etb): if chain: current_hook(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)
sys.excepthook = syslog_exception
I think I'll open an issue for this. I don't think the language moratorium would preclude its addition.
Eric.
-- Eric.
On 03/23/2010 02:24 PM, Eric Smith wrote:
On second thought, maybe it would be better to optionally chain to the existing sys.excepthook instead of assuming it writes to stderr. The above code would become:
That looks just great. I understand you're leaving tomorrow, so I'll probably try converting it to C while you're away. I expect I should be able to find some time. I'll make an issue for it if you haven't already, and submit it for review there.
I don't imagine it will be too hard, but I haven't really done any C API hacking at the level of creating an inner function. I should be able to figure it out though.
Thanks, and have a good trip.
Sean Reifschneider, Member of Technical Staff jafo@tummy.com tummy.com, ltd. - Linux Consulting since 1995: Ask me about High Availability
Sean Reifschneider wrote:
On 03/23/2010 02:24 PM, Eric Smith wrote:
On second thought, maybe it would be better to optionally chain to the existing sys.excepthook instead of assuming it writes to stderr. The above code would become:
That looks just great. I understand you're leaving tomorrow, so I'll probably try converting it to C while you're away. I expect I should be able to find some time. I'll make an issue for it if you haven't already, and submit it for review there.
I don't imagine it will be too hard, but I haven't really done any C API hacking at the level of creating an inner function. I should be able to figure it out though.
Thanks, and have a good trip.
Thanks, Sean.
It's issue 8214, you should already be nosy on it. I used a slightly different method there, designed to remove a reference to sys.excepthook if not chaining, but the principle is the same.
I'm not sure about the closure aspect of this in C. You could do it with a global variable, but I'm not wild about that. Maybe you can ask on python-dev for suggestions.
I'll be checking email occasionally while I'm away.
-- Eric.
On 03/24/2010 04:24 PM, Eric Smith wrote:
I'm not sure about the closure aspect of this in C. You could do it with a global variable, but I'm not wild about that. Maybe you can ask on python-dev for suggestions.
I'll probably do some digging into the API to see about creating a function via C, and from that the issue of the closure will probably become more clear.
If not, python-dev will be my go-to-guy. :-)
Sean Reifschneider, Member of Technical Staff jafo@tummy.com tummy.com, ltd. - Linux Consulting since 1995: Ask me about High Availability