Python Can';t禁用烧瓶/werkzeug记录

Python Can';t禁用烧瓶/werkzeug记录,python,Python,我已经尝试了很长一段时间来禁用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_mo

我已经尝试了很长一段时间来禁用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上使用Python3.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服务器日志显然有点特殊:

[…]伐木工人可能不合作。例如,socket和syslog处理程序使用socket模块的方式可能会阻塞,大多数处理程序都会获取线程锁

如果您想对gevent WSGI服务器日志记录有更多的控制权,可以传入您自己的日志记录程序(以及
error\u 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',) {}

但是当然,禁用这些日志程序中的任何一个都不起作用。

grep
应该可以与正常的python控制台输出一起工作:)但这与您的主要问题无关,即首先停止上述日志。我尝试使用python3 app.py | grep-v-I“post”它仍然给了我这些信息。我对werkzeug不太了解,这个记录器应该写入文件还是stdout/stderr?grep将只对后者进行操作。请尝试将此行放在'werkzeug'记录器启动-logging.basicConfig(level=logging.DEBUG)@G.Ballegeer之前。是否已修复?我也面临同样的问题。