Skip to content
This repository was archived by the owner on Oct 27, 2022. It is now read-only.
This repository was archived by the owner on Oct 27, 2022. It is now read-only.

UnicodeEncodeError in logging statements #19

@volnt

Description

@volnt

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 ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions