Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

logging fails with unicode chars #96

Closed
ttaghavi opened this issue Aug 4, 2014 · 4 comments
Closed

logging fails with unicode chars #96

ttaghavi opened this issue Aug 4, 2014 · 4 comments

Comments

@ttaghavi
Copy link

ttaghavi commented Aug 4, 2014

Error traceback (last part):

File "/data/webadmin/.virtualenvs/ui/lib/python2.7/site-packages/nodeenv.py", line 144, in emit
self.stream.write(fs % msg)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2018' in position 42: ordinal not in range(128)

Since the encode/decode functions always confused me since I started using python, not sure what the best way would be to fix this.

What worked for me was:

self.stream.write(fs % msg.encode('utf8'))

But this will fail if msg is of type 'str'

@jhermann
Copy link
Contributor

jhermann commented Aug 4, 2014

A helper like this is relatively robust and fixes the problem most of the time…

https://github.com/jhermann/waif/blob/master/python/to_uft8.py

@ekalinin
Copy link
Owner

@ttaghavi, Thanks for the report!
@jhermann, Thanks for the link! Seems it will be very useful here.

And seems this bug is correlated with #49

@ralcini
Copy link

ralcini commented Jul 20, 2015

+1

@ralcini
Copy link

ralcini commented Jul 20, 2015

stream seems support only ascii strings, non ascii chars can be ignored or replaced with placeholder. What do you think?

--- lib/python2.6/site-packages/nodeenv.py  2015-07-16 17:07:09.000000000 +0200
+++ lib/python2.6/site-packages/nodeenv.py  2015-07-16 17:08:07.344132749 +0200
@@ -152,7 +152,7 @@
     def emit(self, record):
         msg = self.format(record)
         fs = "%s" if getattr(record, "continued", False) else "%s\n"
-        self.stream.write(fs % msg)
+        self.stream.write(fs % msg.encode('ascii', 'ignore'))
         self.flush()
     logging.StreamHandler.emit = emit

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants