google / protorpc

Apache License 2.0
68 stars 37 forks source link

UnicodeEncodeError in logging statements #19

Open volnt opened 8 years ago

volnt commented 8 years ago

Hello,

I am using appengine and I noticed that some of my requests crashed without outputing any logs.

After investigating I found some logs that were not forwarded to the output :

File "/home/vmagent/python_vm_runtime/lib/protorpc-1.0/protorpc/wsgi/service.py", line 191, in protorpc_service_app
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 3604468: ordinal not in range(128)

It's happening here : https://github.com/google/protorpc/blob/master/protorpc/wsgi/service.py#L190

When the error raised is a unicode instead of an str it's crashing. You can reproduce the issue easily with this piece of code :

In  [1]: try:                                                      
   ....:         raise ValueError(u'sample unicode : \xa0')
   ....: except Exception as err:
   ....:         logging.exception('%s %s' % ('text', err))
   ....:     
---------------------------------------------------------------------------
UnicodeEncodeError                        Traceback (most recent call last)
<ipython-input-22-6518b912bd3d> in <module>()
      2         raise ValueError(u'sample unicode : \xa0')
      3 except Exception as err:
----> 4         logging.exception('%s %s' % ('text', err))
      5 

UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 17: ordinal not in range(128)

In order to fix this you just have to add a u before the string in the logging statement :

In  [2]: try:                                                      
        raise ValueError(u'sample unicode : \xa0')
except Exception as err:
        logging.exception(u'%s %s' % ('text', err))
   ....:     
ERROR:root:text sample unicode :  
Traceback (most recent call last):
  File "<ipython-input-23-fded89cbdcf2>", line 2, in <module>
    raise ValueError(u'sample unicode : \xa0')
ValueError: sample unicode : \xa0

Is that something that could be fixed ? Would you accept a PR with this fix ?

craigcitro commented 8 years ago

Sure, if you sign the CLA, happy to accept a PR.

As a heads-up, this project is mostly dormant, so I don't know how soon we'll cut a release.