简体   繁体   English

如何让 Flask 记录到 stdout 而不是 stderr?

[英]How to make Flask log to stdout instead of stderr?

Flask does all kinds of logging automatically, for example when receiving a POST request Flask will automatically log it: Flask 会自动进行各种日志记录,例如当收到 POST 请求时 Flask 会自动记录它:

 127.0.0.1 - - [05/Jul/2019 18:18:16] "POST /test/ HTTP/1.1" 200 -

The problem is that this logging is done to stderr , I would like it to instead do all the same logging, with the default formatting, but log to sys.stdout instead.问题是此日志记录是针对stderr完成的,我希望它使用默认格式执行所有相同的日志记录,但改为记录到sys.stdout

I've tried something like this:我试过这样的事情:

import logging
import sys

app = flask.Flask(__name__)
handler = logging.StreamHandler(sys.stdout)
app.logger.addHandler(handler)

And based on flask documentation I tried:根据我尝试过的烧瓶文档

import sys
from logging.config import dictConfig

dictConfig({
    'version': 1,
    'formatters': {'default': {
        'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
    }},
    'handlers': {'wsgi': {
        'class': 'logging.StreamHandler',
        'stream': 'sys.stdout',
        'formatter': 'default'
    }},
    'root': {
        'level': 'INFO',
        'handlers': ['wsgi']
    }
})

app = flask.Flask(__name__)

But the first one doesn't have the desired effect and the second one just crashes.但是第一个没有达到预期的效果,第二个只是崩溃。

Based on @RomanPerekhrest 's comment, this did the job:根据@RomanPerekhrest的评论,这完成了工作:

import sys
from logging.config import dictConfig

dictConfig({
    'version': 1,
    'formatters': {'default': {
        'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
    }},
    'handlers': {'wsgi': {
        'class': 'logging.StreamHandler',
        'stream': 'ext://sys.stdout',
        'formatter': 'default'
    }},
    'root': {
        'level': 'INFO',
        'handlers': ['wsgi']
    }
})


app = flask.Flask(__name__)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将stdout和stderr添加到烧瓶中的记录器文件中 - How to add stdout and stderr to logger file in flask 如何在Python中异步记录stdout / stderr? - How to asynchronously log stdout/stderr in Python? 如何在后台进程中将stderr和stdout重定向到/ var / log目录? - How to redirect stderr and stdout into /var/log directory in background process? 如何以编程方式告诉 Celery 将所有日志消息发送到 stdout 或 stderr? - How to programmatically tell Celery to send all log messages to stdout or stderr? 如何使 pycharm 在运行 pytest 时将 stdout 和 stderr 转储到控制台? - How to make pycharm dump stdout and stderr to console on running pytest? 如何使子进程按顺序获取stdout和stderr? - How can I make subprocess get the stdout and stderr in order? sh python模块如何记录stderr / stdout并仅捕获stdout? - how can the sh python module log stderr/stdout and only capture stdout? 将stdout和stderr重定向到两个不同的日志文件 - Redirection of stdout and stderr to two different log files 自定义记录器如何仅在较低级别记录到标准输出时记录错误/对标准错误至关重要? - How can a custom logger log error/critical to stderr only while lower levels log to stdout? subprocess.Popen 返回标准错误而不是标准输出 - subprocess.Popen return stderr instead of stdout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM