Logging with WSGI server and flask application Logging with WSGI server and flask application flask flask

Logging with WSGI server and flask application


According to the gevent uwsgi documentation you need to pass your log handler object to the WSGIServer object at creation:

log – If given, an object with a write method to which request (access) logs will be written. If not given, defaults to sys.stderr. You may pass None to disable request logging. You may use a wrapper, around e.g., logging, to support objects that don’t implement a write method. (If you pass a Logger instance, or in general something that provides a log method but not a write method, such a wrapper will automatically be created and it will be logged to at the INFO level.)

error_log – If given, a file-like object with write, writelines and flush methods to which error logs will be written. If not given, defaults to sys.stderr. You may pass None to disable error logging (not recommended). You may use a wrapper, around e.g., logging, to support objects that don’t implement the proper methods. This parameter will become the value for wsgi.errors in the WSGI environment (if not already set). (As with log, wrappers for Logger instances and the like will be created automatically and logged to at the ERROR level.)

so you should be able to do wsgi.WSGIServer(('0.0.0.0', 8080), app, log=app.logger)


You can log like this -

import loggingimport logging.handlers as handlers...logger = logging.getLogger('MainProgram')logger.setLevel(10)logHandler = handlers.RotatingFileHandler(filename.log,maxBytes =1000000, backupCount=1)logger.addHandler(logHandler)logger.info("Logging configuration done")...# run the applicationserver=  wsgi.WSGIServer(('0.0.0.0', 8080), app, log=logger)server.serve_forever()