今天爱分享给大家带来Python logging.FileHandler 文件中文字符乱码【解决办法】,希望能够帮助到大家。
解决方法很简单,在定义Filehandler时指定encoding就好了
import logging file_hanlder = logging.FileHandler(filename='example.log', encoding='utf-8')
因为看python的logging.FileHandler()类的代码如下,就知道Filehanlder也支持指定编码格式的了,原代码如下:
# logging\__init__.py class FileHandler(StreamHandler): """ A handler class which writes formatted logging records to disk files. """ def __init__(self, filename, mode='a', encoding=None, delay=False): """ Open the specified file and use it as the stream for logging. """ # Issue #27493: add support for Path objects to be passed in filename = os.fspath(filename) ...