How to enable logging of Flask app with `gevent.pywsgi.WSGIServer` and `WebSocketHandler`?
Asked Answered
H

2

8

The issue should be reproducible with the following two minimal examples:

Minimal example with app.run()

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
    return 'Hello'

app.run()

Minimal example with gevent.pywsgi.WSGIServer

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
    return 'Hello'

from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler
server = pywsgi.WSGIServer(('127.0.0.1', 5000), app, handler_class=WebSocketHandler)
server.serve_forever()

The first 5 lines are identical, so both examples only differ in the way they start the server. Both servers do work, I get "Hello" in the browser. The first example prints:

 * Serving Flask app "1" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [20/Jun/2019 23:43:15] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [20/Jun/2019 23:43:19] "GET / HTTP/1.1" 200 -

So the console output shows some information about every request which is handled by the server.

However, with the second example, I don't get any logging output in the console anymore. How do I enable logging for gevent.pywsgi.WSGIServer and WebSocketHandler?


Background (which doesn't matter regarding the issue, I think)

I'm running a Flask app which uses flask_sockets. Because

Werkzeug development server cannot provide the WSGI environ with a websocket interface

I am not able to use the server with app.run() and I'm using gevent.pywsgi.WSGIServer instead. The code of my example above is taken directly from the module's examples at https://github.com/heroku-python/flask-sockets without any modifications.

Henning answered 20/6, 2019 at 21:7 Comment(0)
H
0

It should work like that, but it's a bug in WebSocketHandler from the geventwebsocket module which has already been reported: https://gitlab.com/noppo/gevent-websocket/issues/16

Henning answered 24/6, 2019 at 22:29 Comment(0)
N
5

Logging works differently with gevent.pywsgi.WSGIServer. It uses python logging and is much more sophisticated and flexible.

Here's an example:

from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler

import logging
logging.basicConfig(level=logging.INFO)

server = pywsgi.WSGIServer(('127.0.0.1', 5000), app, handler_class=WebSocketHandler)
server.serve_forever() 

And now wherever you want output, you can do:

logging.info("You can see me now...")   

If you want to see startup info from WSGIServer, then set the log level to DEBUG and you can see tons of output.

Needlecraft answered 20/7, 2019 at 1:19 Comment(0)
H
0

It should work like that, but it's a bug in WebSocketHandler from the geventwebsocket module which has already been reported: https://gitlab.com/noppo/gevent-websocket/issues/16

Henning answered 24/6, 2019 at 22:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.