How do I redirect python pyramid waitress logging to stdout on heroku?

こ雲淡風輕ζ 提交于 2019-12-02 16:46:43

问题


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?


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

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



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


来源:https://stackoverflow.com/questions/41175887/how-do-i-redirect-python-pyramid-waitress-logging-to-stdout-on-heroku

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!