Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't disable flask/werkzeug logging

Tags:

python

I have been trying for way to long to disable the logger of werkzeug. I'm trying to create a socketio server in python but werkzeug keeps logging all POST and GET requests. It's really annoying because my logging get's flooded.

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)

Everywhere is see this as the solution but it doesn't stop werkzeug from logging.

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

These are the kind of messages:

::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

I've tried to set the level to only critical, but that didn't help either. I've also tried to use grep to suppress the messages but it seems that grep doesn't work with python console output.

Edit: I'm using python 3.5.2 on linux but had the same problem on 3.6 on windows. werkzeug is 0.14.1, flaks is 0.12.2 and python-socketio is 1.8.4

Edit2: I was able to fix the problem by using grep, the problem was that werkzeug send everything to stderr which should be handled differently in the command line.

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

This gives the result I wanted.

like image 421
G. Ballegeer Avatar asked Feb 28 '18 21:02

G. Ballegeer


People also ask

Does Flask use werkzeug?

Flask wraps Werkzeug, using it to handle the details of WSGI while providing more structure and patterns for defining powerful applications. Werkzeug includes: An interactive debugger that allows inspecting stack traces and source code in the browser with an interactive interpreter for any frame in the stack.

How do I turn off debug mode in Flask?

How do I disable debug mode in flask? In production, setting app. debug = False will disable the toolbar.

What is werkzeug Httpd?

Werkzeug is a comprehensive WSGI web application library. It began as a simple collection of various utilities for WSGI applications and has become one of the most advanced WSGI utility libraries. Werkzeug doesn't enforce any dependencies.


1 Answers

The quick answer is to pass log=None when you create your WSGIServer:

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

The gevent WSGI server logging is apparently a bit special 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.

If you want to have more control over the gevent WSGI server logging, you can pass in your own logger (and error_log). Just make sure to wrap it in a LoggingLogAdapter first:

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()

As a side note, I checked which loggers were instantiated with this little patch to logging.getLogger. Maybe it will be helpful for others trying to understand where log output comes from:

import logging

old_getLogger = logging.getLogger

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

logging.getLogger = getLogger

The output was something like:

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',) {}

But of course disabling any of these loggers doesn't work since the default gevent WSGI logger is just printing directly to stderr.

like image 54
André Laszlo Avatar answered Oct 10 '22 02:10

André Laszlo