[英]How to add stdout and stderr to logger file in flask
I want to log the stdout & stderr to log files, and this is what I tried. 我想将stdout和stderr记录到日志文件中,这就是我尝试过的。
app = Flask(__name__)
app.logger.setLevel(logging.INFO) # use the native logger of flask
app.logger.disabled = False
handler = logging.handlers.RotatingFileHandler(
SYSTEM_LOG_FILENAME,
'a',
maxBytes=1024 * 1024 * 100,
backupCount=20
)
formatter = logging.Formatter(\
"%(asctime)s - %(levelname)s - %(name)s: \t%(message)s")
handler.setFormatter(formatter)
app.logger.addHandler(handler)
@app.route('/')
def hello():
return 'Hello World'
if __name__ == '__main__':
app.run()
Then I would like to log the console output in files. 然后我想在文件中记录控制台输出。 such as 如
* Running on http://127.0.0.1:5000/
127.0.0.1 - - [24/May/2013 14:55:14] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [24/May/2013 14:55:14] "GET /favicon.ico HTTP/1.1" 404 -
what can I do? 我能做什么?
The logging messages you mention don't come from flask's logger, the come from werkzeug
's logger , that means you also need to add your handler to that logger instance to make it work, eg: 您提到的日志消息不是来自于flask的记录器, 来自 werkzeug
的记录器 ,这意味着您还需要将处理程序添加到该记录器实例以使其工作,例如:
log = logging.getLogger('werkzeug')
log.setLevel(logging.INFO)
log.addHandler(handler)
If you look at how werkzeug initializes its logger, you'll see that it adds a default handler only if logging wasn't already set up. 如果你看一下werkzeug如何初始化它的记录器,你会发现只有在尚未设置记录时它才会添加一个默认处理程序。 That means if you set it up before wekzeug does, it won't use the default StreamHandler
but the handler you supply. 这意味着如果你在wekzeug之前设置它,它将不会使用默认的StreamHandler
而是你提供的处理程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.