首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >更改已设置的日志记录的'basicConfig‘

更改已设置的日志记录的'basicConfig‘
EN

Stack Overflow用户
提问于 2012-08-28 19:12:31
回答 1查看 21.2K关注 0票数 44

我使用python中的日志模块作为:

代码语言:javascript
复制
import logging, sys
logger= logging.getLogger(__file__)
logging.basicConfig(stream = sys.stderr, level=logging.DEBUG, format='%(filename)s:%(lineno)s %(levelname)s:%(message)s')
logger.debug("Hello World")

现在,在line 3上设置了基本配置之后,我想要有一个命令行参数,它可以将输出流从sys.stderr更改为文件。

我读过文档,它说如果filenamestream同时存在,stream将被忽略。

现在,我想知道如何在line 3中完成basicConfig操作后将流更改为文件

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-28 19:24:26

如果查看logging/__init__.py的Python源代码,您将看到basicConfig()通过调用addHandler()在根记录器对象上设置处理程序。如果您想从头开始,可以删除所有现有的处理程序,然后再次调用basicConfig()

代码语言:javascript
复制
# Example to remove all root logger handlers and reconfigure. (UNTESTED)
import logging

# Remove all handlers associated with the root logger object.
for handler in logging.root.handlers[:]:
    logging.root.removeHandler(handler)

# Reconfigure logging again, this time with a file.
logging.basicConfig(filename = 'myfile.log', level=logging.DEBUG, format='%(filename)s:%(lineno)s %(levelname)s:%(message)s')
票数 85
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12158048

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档