Logging with command line waitress-serve

8.8k Views Asked by At

Is there a way to log waitress-serve output into a file?

The current command I use is:

waitress-serve --listen=localhost:8080 --threads=1 my_app_api:app

The application we used was not written with waitress in mind earlier, so we choose to serve it with command line to avoid change (for now at least).

1

There are 1 best solutions below

0
On

TLDR waitress-serve doesn't provide a way to do it. See the 'how do i get it to log' section.

Background

Per the documentation for the command-line usage of waitress-serve, no - there's no way to setup logging. See arguments docs.

waitress-serve is just an executable to make running your server more convenient. It's source-code is here runner.py. If you read it, you can see it actually is basically just calling from waitress import serve; serve(**args) for you. (That code clip is not literally what it's doing, but in spirit yes).

The documentation for waitress says that it doesn't log http traffic. That's not it's job. But it will log it's own errors or stacktraces. logging docs. If you read the waitress source trying to find when it logs stuff, you'll notice it doesn't seem to log http traffic anywhere github log search. It primarily logs stuff to do with the socket layer.

Waitress does say that if you want to log http traffic, then you need another component. In particular, it points you to pastedeploy docs which is some middle-ware that can log http traffic for you.

The documentation from waitress is actually kind of helpful answering you question, though not direct and explicit. It says

The WSGI design is modular.

per the logging doc

I.e. waitress won't log http traffic for you. You'll need another WSGI component to do that, and because WSGI is modular, you can probably choose a few things.

If you want some background on how this works, there's a pretty good post here leftasexercise.com

OK, how do I get it to log?

Use tee

Basically, if you just want to log the same stuff that is output from waitress-serve then you don't need anything special.

waitress-serve --listen=localhost:8080 --threads=1 my_app_api:app | tee -a waitress-serve.log

Python logging

But if you're actually looking for logging coming from python's standard logger (say you app is making logger calls or you want to log http traffic) then, you can set that up in your python application code. E.g. edit your applications soure-code and get it to setup logging to a file

import logging
logging.basicConfig(filename='app.log', encoding='utf-8', level=logging.DEBUG)

PasteDeploy middleware for http logs

Or if your looking for apache type http logging then you can use something like PasteDeploy to do it. Note, PasteDeploy is another python dependency so you'll need to install it. E.g.

pip install PasteDeploy

Then you need to setup a .ini file that tells PasteDeploy how to start your server and then also tell it to use TransLogger to create apache type http logs. This is explained more detail here logging with pastedeploy The ini file is specific to each app, but from your question is sounds like the ini file should look like:

[app:wsgiapp]
use = my_app_api:app

[server:main]
use = egg:waitress#main
host = 127.0.0.1
port = 8080

[filter:translogger]
use = egg:Paste#translogger
setup_console_handler = False

[pipeline:main]
pipeline = translogger
           app

You'll still need to edit the source-code of your app to get PasteDeploy to load the app with your configuration file:

from paste.deploy import loadapp
wsgi_app = loadapp('config:/path/to/config.ini')

Webframework-dependent roll-your-own http logging

Even if you want to log http traffic, you don't necessarily need something like PasteDeploy. For example, if you are using flask as the web-framework, you can write your own http logs using after_request decorator:

@app.after_request
def after_request(response):
     timestamp = strftime('[%Y-%b-%d %H:%M]')
     logger.error('%s %s %s %s %s %s', timestamp, request.remote_addr, request.method, request.scheme, request.full_path, response.status)
     return response

See the full gist at https://gist.github.com/alexaleluia12/e40f1dfa4ce598c2e958611f67d28966