[Twisted-Python] Twisted logger changes

Glyph glyph at twistedmatrix.com
Tue Jun 9 00:09:33 MDT 2015


> On Jun 8, 2015, at 9:51 AM, Maciej Wasilak <wasilak at gmail.com> wrote:
> 
> Dear list,
> 
> I've tried running Twisted 15.2.1 with my old library, and I am
> getting an exception, that I haven't seen before (with Twisted
> 14.0.0):
> 
>      File "/usr/local/lib/python2.7/dist-packages/txthings/coap.py",
> line 1338, in sendResponse
>        print "Token: %s" % (response.token)
>      File "/usr/local/lib/python2.7/dist-packages/twisted/logger/_io.py",
> line 163, in write
>        string = string.decode(self._encoding)
>      File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
>        return codecs.utf_8_decode(input, errors, True)
>    exceptions.UnicodeDecodeError: 'utf8' codec can't decode byte 0xae
> in position 7: invalid start byte
> 
> Although it is triggered by simple print, it seems to be caused by
> logging. Does new logging framework need any configuration to stay
> compatible with the old one?


Although logger is reacting to this in a bad way, and that is a bug, your program has a bug too: you are printing to a text file (which is what stdout is when it is redirected to a log) by using binary data.  python 3 will mangle your output if you try to do this, so you should restrict your program to unicode if you intend to redirect stdout to a logger.

Here's a very simple example which provokes the same bad behavior:

import sys
from twisted.logger import globalLogBeginner, textFileLogObserver
globalLogBeginner.beginLoggingTo([textFileLogObserver(sys.stdout)])
print(b"Testing testing, one \xae two.")

The equivalent in old-style logging would be:

import sys
from twisted.python.log import startLogging
startLogging(sys.stdout)
print(b"Testing testing, one \xae two.")

Therefore this is a regression in the new logging system.  I've filed it as a bug, here: <https://twistedmatrix.com/trac/ticket/7933 <https://twistedmatrix.com/trac/ticket/7933>>.  In the future, I would encourage you to take advantage of the pre-release testing period to discover issues like this, since it would have been nice to know about this before the final release went out ;-).  (Not to say this is your fault though, this is totally our bug, and I'm surprised we didn't catch it given all the other similar stuff we caught before the release.)

-glyph

-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20150608/92e4c851/attachment-0002.html>


More information about the Twisted-Python mailing list