Logging with command line waitress-serve
Asked Answered
J

1

6

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).

Janaye answered 17/9, 2018 at 16:30 Comment(5)
@It's not exactly coding problem (although requires knowledge of), and therefore does not require a code example. You're not impressing anyone with fancy URLs. If you can't help, please do everyone a favor, and try to spend your time more productively elsewhere. Thanks.Janaye
At the time I posted, you gave no indication of the way you were using the module (and still really have not). The amount of views or assistance this question has received speaks volumes to how well written or presented it is, and with a rude reply to a suggestion on how to improve the question, I'd expect it simply will never be answered, good luck.Fefeal
waitress-serve is a command line utlity. Anyone who doesn't know this, is probably wont be of much help anyway. Your first comment wasn't very polite either. Thanks for taking the time to write the second one. If it matters, I've added the command I use to launch it.Janaye
But in all seriousness, I'd take a look at #4760715Fefeal
Thanks a lot for the answer. I'll give it a try now (:Janaye
O
7

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

Obligatory answered 11/12, 2020 at 12:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.