gpt4 book ai didi

Python Tornado - 禁用日志记录到 stderr

转载 作者:太空狗 更新时间:2023-10-29 20:33:32 26 4
gpt4 key购买 nike

我有一个简约的 Tornado 应用程序:

import tornado.ioloop
import tornado.web

class PingHandler(tornado.web.RequestHandler):
def get(self):
self.write("pong\n")

if __name__ == "__main__":
application = tornado.web.Application([ ("/ping", PingHandler), ])
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()

Tornado 不断向 stderr 报告错误请求:

WARNING:tornado.access:404 GET / (127.0.0.1) 0.79ms

问题:它想阻止它记录错误信息。怎么办?

Tornado 版本 3.1; Python 2.6

最佳答案

很明显,当我们启动 Tornado 时,“某人”正在初始化日志子系统。以下是来自 ioloop.py 的代码揭示了其中的奥秘:

def start(self):
if not logging.getLogger().handlers:
# The IOLoop catches and logs exceptions, so it's
# important that log output be visible. However, python's
# default behavior for non-root loggers (prior to python
# 3.2) is to print an unhelpful "no handlers could be
# found" message rather than the actual log entry, so we
# must explicitly configure logging if we've made it this
# far without anything.
logging.basicConfig()

basicConfig 被调用并配置默认的 stderr 处理程序。

因此,要为 tonado 访问设置正确的日志记录,您需要:

  1. 将处理程序添加到 tornado.access 记录器:logging.getLogger("tornado.access").addHandler(...)

  2. 禁用上述记录器的传播:logging.getLogger("tornado.access").propagate = False。否则消息将同时到达您的处理程序和 stderr

关于Python Tornado - 禁用日志记录到 stderr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21234772/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com