0

I'm logging some Unicode characters to a file using "logging" in Python 3. The code works in the terminal, but fails with a UnicodeEncodeError in PyCharm.

I load my logging configuration using logging.config.fileConfig. In the configuration, I specify a file handler with encoding = utf-8. Logging to console works fine.

I'm using PyCharm 2019.1.1 (Community Edition). I don't think I've changed any relevant setting, but when I ran the same code in the PyCharm on another computer, the error was not reproduced. Therefore, I suspect the problem is related to a PyCharm setting.

Here is a minimal example:

import logging
from logging.config import fileConfig

# ok
print('1. café')

# ok
logging.error('2. café')

# UnicodeEncodeError
fileConfig('logcfg.ini')
logging.error('3. café')

The content of logcfg.ini (in the same directory) is the following:

[loggers]
keys = root

[handlers]
keys = file_handler

[formatters]
keys = formatter

[logger_root]
level = INFO
handlers = file_handler

[handler_file_handler]
class = logging.handlers.RotatingFileHandler
formatter = formatter
args = ('/tmp/test.log',)
encoding = utf-8

[formatter_formatter]
format = %(levelname)s: %(message)s

I expect to see the first two logging messages in the console, and the third one in the logging file. The first two logging statements worked fine, but the third one failed. Here is the complete console output in PyCharm:

1. café
ERROR:root:2. café
--- Logging error ---
Traceback (most recent call last):
  File "/anaconda3/lib/python3.6/logging/__init__.py", line 996, in emit
    stream.write(msg)
UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 13: ordinal not in range(128)
Call stack:
  File "/Users/klkh/test.py", line 12, in <module>
    logging.error('3. café')
Message: '3. café'
Arguments: ()
klkh
  • 265
  • 1
  • 2
  • 11

2 Answers2

0

You seem to use utf-8 as encode for your config file, python (when using pycharm) in your case seems to raise an encode error UnicodeEncodeError in place of guessing your config file encode wildly, because if it use a wrong encode all the config file gonna be decrypted differently from the original, so best to do is precising the encode type of the config in your python script

Notice : I can't seem to find documentation of fileConfig from logging.config so I'm using basicConfig

import logging
from logging.config import fileConfig

print('1. café')

logging.error('2. café')

logging.basicConfig(filename='your config' , encode='utf-8') # in your case the encode is utf-8
logging.error('3. café')

output:

1. café ERROR:root:2. café ERROR:root:3. café

Mobrine Hayde
  • 365
  • 1
  • 2
  • 10
0

I found a solution myself. I should pass the encoding like this:

args = ('/tmp/test.log', 'a', 0, 0, 'utf-8')

instead of

args = ('/tmp/test.log',)
encoding = utf-8

However, I'm still interested in knowing why the PyCharm on the other computer uses utf-8 by default. How do I set the default encoding for non-console streams in PyCharm?

klkh
  • 265
  • 1
  • 2
  • 11