[Twisted-Python] passing exception object to log.err doesn't log traceback?
I was just looking at some logs and some of the errors logged without tracebacks, I work out it was when I wasn't passing the error object to log.err- Is this by design? Here is some code which shows it logging and not logging the traceback import os from twisted.python import log from twisted.python.logfile import DailyLogFile logfile= DailyLogFile.fromFullPath(os.path.join(os.path.dirname(__file__),"error.log")) log.startLogging(logfile) def zero_error(): 1/0 def stack(x): if not x: zero_error() else: stack(x-1) def work(x,pass_exception): try: stack(x) except Exception as e: log.err(e if pass_exception else None) if __name__ == "__main__": work(5,True) #Doesn't log traceback work(5,False) #Does log
On Thu, Nov 15, 2012 at 9:02 AM, Paul Wiseman <poalman@gmail.com> wrote:
I was just looking at some logs and some of the errors logged without tracebacks, I work out it was when I wasn't passing the error object to log.err- Is this by design?
In Python 2.x, exception objects do not have any reference to their traceback; this is one of the reasons Twisted has the Failure class, which encapsulates both. (In Python 3 exceptions do have their tracebacks attached.) Thus, if you want the traceback logged, you should do something like: from twisted.python import log try: 1/0 except Exception as e: # Log the last exception that occurred, including its traceback: log.err(None, "An error occurred.") Or: from twisted.python import log, failure try: 1/0 except Exception as e: f = failure.Failure() log.err(f, "An error occurred.") -- Itamar Turner-Trauring, Future Foundries LLC http://futurefoundries.com/ — Twisted consulting, training and support.
Ah I doubted it was a bug but the behaviour surprised me. Thanks for explaining! On 15 November 2012 14:56, Itamar Turner-Trauring < itamar@futurefoundries.com> wrote:
On Thu, Nov 15, 2012 at 9:02 AM, Paul Wiseman <poalman@gmail.com> wrote:
I was just looking at some logs and some of the errors logged without tracebacks, I work out it was when I wasn't passing the error object to log.err- Is this by design?
In Python 2.x, exception objects do not have any reference to their traceback; this is one of the reasons Twisted has the Failure class, which encapsulates both. (In Python 3 exceptions do have their tracebacks attached.) Thus, if you want the traceback logged, you should do something like:
from twisted.python import log
try: 1/0 except Exception as e: # Log the last exception that occurred, including its traceback: log.err(None, "An error occurred.")
Or:
from twisted.python import log, failure
try: 1/0 except Exception as e: f = failure.Failure() log.err(f, "An error occurred.")
-- Itamar Turner-Trauring, Future Foundries LLC http://futurefoundries.com/ — Twisted consulting, training and support.
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
participants (2)
-
Itamar Turner-Trauring
-
Paul Wiseman