Skip to content Skip to sidebar Skip to footer

How Do I Redirect Python Pyramid Waitress Logging To Stdout On Heroku?

According to the docs (http://docs.pylonsproject.org/projects/waitress/en/latest/): Waitress sends its logging output (including application exception renderings) to the Python

Solution 1:

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

defsimple_tween_factory(handler, registry):
    # one-time configuration code goes heredefsimple_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 responsereturn response

return simple_tween

Add this to your Pyramid Configuration

config.add_tween('{}.tweens.simple_tween_factory'.format(<your app name>))

Solution 2:

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.deployimport loadapp
import pyramid.pasterfrom 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)

Post a Comment for "How Do I Redirect Python Pyramid Waitress Logging To Stdout On Heroku?"