gpt4 book ai didi

python - 如何为 pyramid/pylons 2 编写日志记录中间件?

转载 作者:可可西里 更新时间:2023-11-01 09:07:49 25 4
gpt4 key购买 nike

我想使用 mongodb 或 redis 为 Pyramid/塔中的用户保留日志,但找不到有关创建中间件的文档。我该怎么做?

最佳答案

标准中间件

class LoggerMiddleware(object):
'''WSGI middleware'''

def __init__(self, application):

self.app = application

def __call__(self, environ, start_response):

# write logs

try:
return self.app(environ, start_response)
except Exception, e:
# write logs
pass
finally:
# write logs
pass

在 Pyramid 中创建应用程序代码:

from paste.httpserver import serve
from pyramid.response import Response
from pyramid.view import view_config

@view_config()
def hello(request):
return Response('Hello')

if __name__ == '__main__':
from pyramid.config import Configurator
config = Configurator()
config.scan()
app = config.make_wsgi_app()

# Put middleware
app = LoggerMiddleware(app)

serve(app, host='0.0.0.0')

关于python - 如何为 pyramid/pylons 2 编写日志记录中间件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4936507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com