Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

533
Views
How to enable logging of Flask app with `gevent.pywsgi.WSGIServer` and `WebSocketHandler`?

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.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

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

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!