[Twisted-Python] passing exception object to log.err doesn't log traceback?

Paul Wiseman poalman at gmail.com
Thu Nov 15 11:28:00 MST 2012


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 at futurefoundries.com> wrote:

>
>
> On Thu, Nov 15, 2012 at 9:02 AM, Paul Wiseman <poalman at 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 at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20121115/6d10669f/attachment-0001.html>


More information about the Twisted-Python mailing list