Search code examples
pythonloggingwsgiwaitresshug

Logging with hug and waitress


I want to add logging to my Python hug REST app. I couldn't find any wayto do it when serving the app through the hug command (via hug -f app.py), therefore I try to combine hug with waitress.

My minimal app structure in the file app.py looks like:

import logging
logger = logging.getLogger(__name__)
import hug
.
.
.
@hug.get()
def func(detail):
    logger.debug("debug func")
    .
    .
    .

And I serve this with a waitress script run.py:

import logging
import waitress

import app

logger = logging.getLogger('waitress')
logger.setLevel(logging.DEBUG)
logger.debug("logger set to DEBUG")

waitress.serve(app.__hug_wsgi__)

When I execute python run.py in a console, the app spins up nicely and the results of func are served back, however the debug messages from inside func ("debug func") and from run.py ("logger set to DEBUG") I cannot see in the console.

What is going wrong and how can I fix it? (I'm happy to use another (Windows-capable) WSGI server if that's easier.)


Solution

  • You have to configure logging for the logging module. Take a look at the documentation for logging.config (in particular dictConfig and fileConfig). As a start, to test whether it works, you can simply call

    logging.basicConfig()
    

    in app.py before you do any logging. This will make the output of all channels go to sys.stderr.

    Don't forget to do logging.setLevel(logging.DEBUG) in app.py too if you want the debug message there to be visible.