Python Tornado - 禁用记录到 stderr [英] Python Tornado - disable logging to stderr

查看:27
本文介绍了Python Tornado - 禁用记录到 stderr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有简约的 Tornado 应用程序:

I have minimalistic Tornado application:

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 报告错误请求:

Tornado keeps reporting error requests to stderr:

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

问题:它想阻止它记录错误消息.怎么样?

Question: It want to prevent it from logging error messages. How?

龙卷风3.1版;Python 2.6

Tornado version 3.1; Python 2.6

推荐答案

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

Its clear that "someone" is initializing logging subsystem when we start Tornado. Here is the code from ioloop.py that reveals the mystery:

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 处理程序.

basicConfig is called and configures default stderr handler.

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

So to setup proper logging for tonado access, you need to:

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

  1. Add a handler to tornado.access logger: logging.getLogger("tornado.access").addHandler(...)

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

Disable propagation for the above logger: logging.getLogger("tornado.access").propagate = False. Otherwise messages will arrive BOTH to your handler AND to stderr

这篇关于Python Tornado - 禁用记录到 stderr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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