WSGI support for FirePython import logging import firepython class Fir

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# WSGI support for FirePython
import logging
import firepython
class FirePythonWSGI(object):
"""
WSGI middleware to enable FirePython logging.
"""
def __init__(self, app):
self.app = app
self.handler = firepython.FirePythonLogHandler()
self.root = logging.getLogger()
def __call__(self, environ, start_response):
self.root.addHandler(self.handler)
self.root.setLevel(logging.DEBUG)
# collect headers
resp_info = []
def faked_start_response(status, headers, exc_info=None):
# collect logs
headers = []
def add_header(name, value):
headers.append((name, value))
self.root.removeHandler(self.handler)
self.handler.flush(add_header)
start_response(*resp_info)
# run app
app_iter = self.app(environ, faked_start_response)
return app_iter