gpt4 book ai didi

python - 无法禁用 Flask/werkzeug 日志记录

转载 作者:行者123 更新时间:2023-12-02 03:29:57 24 4
gpt4 key购买 nike

我一直在尝试禁用 werkzeug 的记录器。我正在尝试用 python 创建一个 socketio 服务器,但 werkzeug 不断记录所有 POST 和 GET 请求。这真的很烦人,因为我的日志被淹没了。

async_mode = 'gevent'
import logging

from flask import Flask, render_template
import socketio


sio = socketio.Server(logger=False, async_mode=async_mode)
app = Flask(__name__)


app.wsgi_app = socketio.Middleware(sio, app.wsgi_app)
app.config['SECRET_KEY'] = 'secret!'
thread = None

app.logger.disabled = True
log = logging.getLogger('werkzeug')
log.disabled = True


@app.route('/')
def index():
#global thread
#if thread is None:
# thread = sio.start_background_task(background_thread)
return render_template('index.html')



@sio.on('answer', namespace='/test')
def test_answer(sid, message):
print(message)


if __name__ == '__main__':
if sio.async_mode == 'threading':
# deploy with Werkzeug
app.run(threaded=True)
elif sio.async_mode == 'eventlet':
# deploy with eventlet
import eventlet
import eventlet.wsgi
eventlet.wsgi.server(eventlet.listen(('', 5000)), app)
elif sio.async_mode == 'gevent':
# deploy with gevent
from gevent import pywsgi
try:
from geventwebsocket.handler import WebSocketHandler
websocket = True
except ImportError:
websocket = False
if websocket:
pywsgi.WSGIServer(('', 5000), app,
handler_class=WebSocketHandler).serve_forever()
else:
pywsgi.WSGIServer(('', 5000), app).serve_forever()
elif sio.async_mode == 'gevent_uwsgi':
print('Start the application through the uwsgi server. Example:')
#print('uwsgi --http :5000 --gevent 1000 --http-websockets --master '
# '--wsgi-file app.py --callable app')
else:
print('Unknown async_mode: ' + sio.async_mode)

到处都将此视为解决方案,但它并不能阻止 werkzeug 进行日志记录。

app.logger.disabled = True
log = logging.getLogger('werkzeug')
log.disabled = True

这些是消息类型:

::1 - - [2018-02-28 22:09:03] "GET /socket.io/?EIO=3&transport=polling&t=M7UFq6u HTTP/1.1" 200 345 0.000344
::1 - - [2018-02-28 22:09:03] "POST /socket.io/?EIO=3&transport=polling&t=M7UFq7A&sid=daaf8a43faf848a7b2ae185802e7f164 HTTP/1.1" 200 195 0.000284
::1 - - [2018-02-28 22:09:03] "GET /socket.io/?EIO=3&transport=polling&t=M7UFq7B&sid=daaf8a43faf848a7b2ae185802e7f164 HTTP/1.1" 200 198 0.000153
::1 - - [2018-02-28 22:10:03] "GET /socket.io/?EIO=3&transport=polling&t=M7UFq7N&sid=daaf8a43faf848a7b2ae185802e7f164 HTTP/1.1" 400 183 60.058020

我尝试将级别设置为仅严重,但这也没有帮助。我还尝试使用 grep 来抑制消息,但 grep 似乎不适用于 python 控制台输出。

编辑:我在 Linux 上使用 python 3.5.2,但在 Windows 3.6 上遇到了同样的问题。 werkzeug 是 0.14.1,flaks 是 0.12.2,python-socketio 是 1.8.4

Edit2:我能够通过使用 grep 解决问题,问题是 werkzeug 将所有内容发送到 stderr,这应该在命令行中以不同的方式处理。

python app.py 2>&1 | grep -v 'GET\|POST'

这给出了我想要的结果。

最佳答案

快速的答案是在创建 WSGIServer 时传递 log=None:

pywsgi.WSGIServer(('', 5000), app, log=None).serve_forever()

gevent WSGI 服务器日志记录显然有点特殊 according to the documentation :

[...] loggers are likely to not be gevent-cooperative. For example, the socket and syslog handlers use the socket module in a way that can block, and most handlers acquire threading locks.

如果您想更好地控制 gevent WSGI 服务器日志记录,您可以传入您自己的记录器(和 error_log)。只需确保首先将其包装在 LoggingLogAdapter 中即可:

from gevent.pywsgi import LoggingLogAdapter
server_log = LoggingLogAdapter(logging.getLogger(__file__))
# server_log.disabled = True # Now you can disable it like a normal log
...
pywsgi.WSGIServer(('', 5000), app, log=server_log).serve_forever()
<小时/>

作为旁注,我检查了使用 logging.getLogger 的这个小补丁实例化了哪些记录器。也许这对于其他试图了解日志输出来自何处的人会有所帮助:

import logging

old_getLogger = logging.getLogger

def getLogger(*args, **kwargs):
print('Getting logger', args, kwargs)
return old_getLogger(*args, **kwargs)

logging.getLogger = getLogger

输出类似于:

Getting logger ('concurrent.futures',) {}
Getting logger ('asyncio',) {}
Getting logger ('engineio.client',) {}
Getting logger ('engineio.server',) {}
Getting logger ('socketio.client',) {}
Getting logger ('socketio',) {}
Getting logger ('socketio',) {}
Getting logger ('socketio',) {}
Getting logger ('socketio.server',) {}
Getting logger ('socketio.client',) {}
Getting logger () {}
Getting logger ('main',) {}
Getting logger ('flask.app',) {}
Getting logger ('flask',) {}
Getting logger ('werkzeug',) {}
Getting logger ('wsgi',) {}

但是,当然禁用任何这些记录器都不起作用,因为 the default gevent WSGI logger is just printing directly to stderr .

关于python - 无法禁用 Flask/werkzeug 日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49038678/

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