[Flask] Logging always ASCII?

Cameron Simpson cs at cskk.id.au
Fri Jul 26 20:33:05 EDT 2019


On 26Jul2019 10:27, Skip Montanaro <skip.montanaro at gmail.com> wrote:
>I have a Flask app (Python 3.6.8, Flask 1.0.2) running behind uwsgi
>(should that make a difference) which seems constitutionally incapable
>of logging UTF-8 strings. I'm not fussing with the app logger Flask
>creates other than to set PROD_LOG_FORMAT and DEBUG_LOG_FORMAT
>attributes ahead of app creation. I am running with
>PYTHONIOENCODING="utf-8" and log that fact:
>
>[2019-07-26 10:11:54,537] INFO in server: IO encoding: utf-8

How is that established? By querying some attribute of sys.stderr?

>Standard output and standard error are redirected to a file (logging
>should be to stderr I think). This suggests that sys.stderr should
>accept UTF-8:
>
>% PYTHONIOENCODING=utf-8 python
>Python 3.6.8 |Anaconda, Inc.| (default, Dec 30 2018, 01:22:34)
>[GCC 7.3.0] on linux
>Type "help", "copyright", "credits" or "license" for more information.
>>>> sys.stderr
><_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>

What about this invocation?

  % PYTHONIOENCODING=utf-8 python 2>some_filename

i.e. not inheriting the terminal stderr.

>Still, if I attempt to log a message which incorporates some UTF-8
>values, it raises UnicodeEncodeError:
>
>UnicodeEncodeError: 'ascii' codec can't encode characters in position
>65-85: ordinal not in range(128).
>
>I could understand if I perhaps was mistaken about the problematic
>value not being UTF-8, but why is the ascii codec involved here?

Do you know if this is happening at write-out time or deeper inside the 
logging module when it is assembling the message string?

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Flask mailing list