[Flask] Logging always ASCII?

Skip Montanaro skip.montanaro at gmail.com
Fri Jul 26 11:27:28 EDT 2019


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

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'>

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?

Thanks,

Skip Montanaro


More information about the Flask mailing list