python - 将编码参数添加到 logging.basicConfig

标签 python logging

如何将 encoding 参数添加到 logging.basicConfig

我找到了 this bug report声明这对于 Python 3.3 现在是可能的。我需要这个用于 Python 2.7,错误报告说使用自定义 logging.FileHandler对象,但我无法让它工作。

最佳答案

在您的案例中避免使用 basicConfig() 会更容易 - 只需创建处理程序并以编程方式添加它(确保代码只运行一次),例如:

root_logger= logging.getLogger()
root_logger.setLevel(logging.DEBUG) # or whatever
handler = logging.FileHandler('test.log', 'w', 'utf-8') # or whatever
handler.setFormatter(logging.Formatter('%(name)s %(message)s')) # or whatever
root_logger.addHandler(handler)

这或多或少就是 basicConfig() 所做的。

更新:在Python 3.9及之后的版本中,basicConfig()encodingerrors关键字参数可用.

关于python - 将编码参数添加到 logging.basicConfig,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10706547/

相关文章:

python - 如何从 "year","date","month","time"列生成unix时间列,最好使用python的pandas模块

java - 如何检查 Java.util.logging 中启用的信息日志级别

c - 如何在 c 中为日志获取适当的时间戳?

elasticsearch - 流利的日志充满了反斜杠,并且kibana没有显示k8s pod日志

symfony - Gedmo 可记录工作但不存储用户名

java - 如何使用 Log4j2 xml Rewrite appender 在登录文件之前修改 LogEvent

python - TensorFlow 未编译为使用 SSE(等)指令,但这些指令可用

python - Python For 循环中的元组和迭代器

python - Pandas:旋转和绘图工作流程

python - 过滤 Pandas 数据框的行不起作用