Flask可同时打印到控制台和日志文件

Flask可同时打印到控制台和日志文件

本文介绍了Python,Flask可同时打印到控制台和日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在使用python 3.7.3,以及烧瓶版本1.0.2.

I'm using python 3.7.3, with flask version 1.0.2.

运行没有以下导入的app.py文件时:

When running my app.py file without the following imports:

import logging
logging.basicConfig(filename='api.log',level=logging.DEBUG)

Flask将向控制台显示相关的调试信息,例如POST/GET请求以及它们来自哪个IP.

Flask will display relevant debug information to console, such as POST/GET requests and which IP they came from.

启用DEBUG日志记录后,我将不再收到此输出.我尝试以调试模式运行应用程序:

As soon as DEBUG logging is enabled, I no longer receive this output. I have tried running my application in debug mode:

app.run(host='0.0.0.0', port=80, debug=True)

但这会产生相同的结果.有没有办法同时启用控制台输出和python日志记录?这听起来像是一个愚蠢的请求,但是我想使用控制台进行演示,同时提供日志文件以进行故障排除.

But this produces the same results. Is there a way to have both console output, and python logging enabled? This might sound like a silly request, but I would like to use the console for demonstration purposes, while having the log file present for troubleshooting.

推荐答案

找到了解决方案:

import logging
from flask import Flask

app = Flask(__name__)

logger = logging.getLogger('werkzeug') # grabs underlying WSGI logger
handler = logging.FileHandler('test.log') # creates handler for the log file
logger.addHandler(handler) # adds handler to the werkzeug WSGI logger

@app.route("/")
def index():
    logger.info("Here's some info")
    return "Hello World"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80)

其他示例:

# logs to console, and log file
logger.info("Some text for console and log file")

# prints exception, and logs to file
except Exception as ue:
        logger.error("Unexpected Error: malformed JSON in POST request, check key/value pair at: ")
        logger.error(ue)

来源: https://docstrings.wordpress.com/2014/04/19/flask-access-log-write-requests-to-file/

如果链接断开:

您可能会感到困惑,因为在Flask的app.logger中添加处理程序无法捕获您在控制台中看到的输出,例如:

You may be confused because adding a handler to Flask’s app.logger doesn’t catch the output you see in the console like:

127.0.0.1 - - [19/Apr/2014 18:51:26] "GET / HTTP/1.1" 200 -

这是因为app.logger适用于Flask,并且输出来自基础WSGI模块Werkzeug.

This is because app.logger is for Flask and that output comes from the underlying WSGI module, Werkzeug.

要访问Werkzeug的记录器,我们必须调用logging.getLogger()并将其命名为Werkzeug使用的名称.这使我们可以使用以下命令将请求记录到访问日志中:

To access Werkzeug’s logger we must call logging.getLogger() and give it the name Werkzeug uses. This allows us to log requests to an access log using the following:

logger = logging.getLogger('werkzeug')
handler = logging.FileHandler('access.log')
logger.addHandler(handler)

# Also add the handler to Flask's logger for cases
#  where Werkzeug isn't used as the underlying WSGI server.
#  This wasn't required in my case, but can be uncommented as needed
#  app.logger.addHandler(handler)

您当然可以添加自己的格式设置和其他处理程序.

You can of course add your own formatting and other handlers.

这篇关于Python,Flask可同时打印到控制台和日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 08:58