[Python-ideas] Adding exception logging function to syslog module.
Eric Smith
eric at trueblade.com
Tue Mar 23 21:24:41 CET 2010
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.
More information about the Python-ideas
mailing list