How do I redirect python pyramid waitress logging

2020-05-01 07:30发布

According to the docs (http://docs.pylonsproject.org/projects/waitress/en/latest/):

Waitress sends its logging output (including application exception renderings) to the Python logger object named waitress. You can influence the logger level and output stream using the normal Python logging module API.

That's all the documentation has to say. I'm trying to redirect logging output to stdout. How would I go about doing that in the pastedeploy .ini file?

2条回答
2楼-- · 2020-05-01 08:06

So, the problem appears to be that the official heroku migration documentation (http://docs.pylonsproject.org/projects/pyramid_cookbook/en/latest/deployment/heroku.html) fails to tell you that you need to initialize the logger for waitress before the app is initialized. Otherwise, nothing gets logged.

So, my question was incorrect. It's not that logging was going to stderr, it was that logging was going nowhere.

The solution is to make the following changes to your runapp.py file.

import os

#from paste.deploy import loadapp
import pyramid.paster
from waitress import serve

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 5000))
    #app = loadapp('config:production.ini', relative_to='.')
    pyramid.paster.setup_logging('production.ini')
    app = pyramid.paster.get_app('production.ini')

    serve(app, host='0.0.0.0', port=port)
查看更多
走好不送
3楼-- · 2020-05-01 08:11

Are you trying to log the requests to waitress? I could never get it working also. If fact I asked the question on SO a couple of years ago and someone said it couldn't be done.

For a work around I added a pyramid "subscriber" that does all the logging for each request.

Edit.

On reviewing my code I stopped using a subscriber and switched to using a tween for logging. Both ways will work.

tweens.py

import logging
log = logging.getLogger(__name__)

def simple_tween_factory(handler, registry):
    # one-time configuration code goes here

     def simple_tween(request):
        # code to be executed for each request before
        # the actual application code goes herE                                  

           response = handler(request)

    # code to be executed for each request after
    # the actual application code goes here

           path_qs = request.path_qs
           status = response.status
           log.debug('[{0}] {1}'.format(status, path_qs))                  ##See the path and query string for every response

           return response

return simple_tween

Add this to your Pyramid Configuration

config.add_tween('{}.tweens.simple_tween_factory'.format(<your app name>))
查看更多
登录 后发表回答