将编码参数添加到logging.basicConfig [英] Add encoding parameter to logging.basicConfig

查看:153
本文介绍了将编码参数添加到logging.basicConfig的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I add an encoding parameter to logging.basicConfig?

我发现此错误报告指出现在对于Python 3.3来说是可能的.我需要Python 2.7,并且错误报告说要使用自定义 对象,但无法正常工作.

I have found this bug report that states that this is now possible for Python 3.3. I need this for Python 2.7 and the bug report says to use a custom logging.FileHandler object, but I can't get it to work.

推荐答案

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

It will be easier to avoid using basicConfig() in your case - just create the handler and add it programmatically (ensuring that the code runs just once), e.g.:

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关键字.

Update: As of Python 3.9 (still in development), basicConfig() should have encoding and errors keywords available.

这篇关于将编码参数添加到logging.basicConfig的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆