Python 官方文档:入门教程 => 点击学习
python版本3.6 解决方法很简单,在定义Filehandler时指定encoding就好了 import logging file_hanlder = logging.FileHandler(filename='exampl
python版本3.6
解决方法很简单,在定义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)
...
以下是后话(多余的话)
遇到这个问题的时候,求教于百度&Google,网上一致给了这种方法:
import logging.handler #3.6版本必须要导入
file_handler = logging.handler.BaseRotatingHandler(filename='example.log', mode='a', encoding='utf-8')
试了一下日志文件中并没有日志打印出来。目前也没有好好研究BaseRotatingHandler是干嘛的。。
感悟:遇到问题,第一步尽量先看python原代码。
--结束END--
本文标题: python logging输入文件中文
本文链接: https://www.lsjlt.com/news/188406.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0