5

I try logging exceptions in Python 2.5, but I can't do it. All formatting functions do something else than what I want.

I came up with this:

def logexception(type, value, traceback):
  print traceback.format_exception(type, value, traceback)

sys.excepthook = logexception

but it bails out with an argument error when called, though according to the docs it should work. Anyone knows what the problem is with this or have an other drop-in solution?

2 Answers 2

5

Why should that traceback argument have a format_exception method just like the function in the traceback module whose name it's usurping, and if it had one why would that method require the same object on which it's called to be passed in as the last argument as well?

I suspect you just want to give the third argument a different name, so as not to hide the traceback module, say:

import sys
import traceback

def logexception(type, value, tb):
  print traceback.format_exception(type, value, tb)

sys.excepthook = logexception

and things should work much better.

Sign up to request clarification or add additional context in comments.

4 Comments

It's embarrassing. One of those errors you can't spot in your own code, but someone else instantly does. :) Thanks.
Since I submitted the same answer (a minute later) I'll delete my answer. For the sake of completeness: since type as argument also shadows a builtin I think it should also probably be renamed to exc_type or whatever...
True. I renamed that as well. Thank you.
@pythonuser, you're welcome; please remember to accept this answer if it has solved your problem (that's core SO etiquette). @ChristopheD, tx for removing the duplication, and I agree that shadowing built-ins is never a good practice even when it doesn't directly hurt (was just trying to keep things focused on pythonuser's specific likely problem!).
-1

you can use this very simpe logging solution

or this one

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.