有没有办法使用 logging
模块使 Python 日志记录自动输出到标准输出 以及 它们应该去的日志文件? For example, I’d like all calls to logger.warning
, logger.critical
, logger.error
to go to their intended places but in addition always be copied to stdout
.这是为了避免重复消息,例如:
mylogger.critical("something failed")
print "something failed"
原文由 user248237 发布,翻译遵循 CC BY-SA 4.0 许可协议
所有日志记录输出都由处理程序处理;只需将
logging.StreamHandler()
添加到根记录器即可。这是一个配置流处理程序的示例(使用
stdout
而不是默认的stderr
)并将其添加到根记录器: