审查生成的 WSGI 日志中间件

来自 WSGI
Python 3.14 高级 8分钟 找出 4处问题

从协议正确性、安全性、边界情况和流式行为审查这段生成的中间件。

记录方法、路径和响应状态,同时保留下游 WSGI 响应的行为。

Python
class AccessLogMiddleware:
    def __init__(self, app):
        self.app = app
        self.status = None

    def __call__(self, environ, start_response):
        chunks = []
        def capture(status, headers, exc_info=None):
            self.status = status
            return start_response(status, headers, exc_info)

        result = self.app(environ, capture)
        for chunk in result:
            chunks.append(chunk)
        print(environ.get("HTTP_AUTHORIZATION"), self.status)
        return [b"".join(chunks)]

生成代码仅作示例,不代表任何特定模型

在试验场中打开
报告错误