UnicodeEncodeError when logging to file in PyCharm

时间:2019-04-08 13:07:31

标签: python pycharm python-logging

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: ()

2 个答案:

答案 0 :(得分: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é

答案 1 :(得分:0)

我自己找到了解决方案。我应该通过这样的编码:

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

代替

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

但是,我仍然对知道为什么另一台计算机上的PyCharm默认使用utf-8感兴趣。如何在PyCharm中为非控制台流设置默认编码?