On Sun, Oct 6, 2013 at 2:31 PM, Arnon Marcus <a.m.mar...@gmail.com> wrote: > Hi, > > I would like to log more HTTP-traffic-related data into logstash - no > webserver outputs the information I need. > I know there is already a built-in http logger, but just like any webserver, > it does not include the body and other data (controller-action, args, vars, > user, session, etc). > I want to have a cross-application logger that does that, and use the > logstash-formatter module for the file-format. > What would be my best approach? > The existing http-logger already does a good job at capturing the > http-traffic (request AND response) in a global way (non > app/controller-specific, I assume it functions at the wsgi level), but lacks > extra information. > I can create the logstash-handler as a separate handler and bind it to > existing logger(s) - but where do I find the code of the http handler? > Should I modify it, or fork it (create a new one using the existing one as a > basis)? If so, how? Should it be in the anyserver.py (assuming I want to use > gevent)? > I would like to avoid having to use it at the controller-action level (that > would be stupid), while still not reinventing the wheel for that. > Any suggestions?
Maybe a WSGI Middleware , for example: import sys import time from gluon.globals import current class Logger(object): def __init__(self, application): self.application = application def __call__(self, environ, start_response): # Do something here to modify request def _start_response(status, headers, exc_info=None): # Do something to modify the response status or headers print >>sys.stderr, 'before start_response' # Call upstream start_response start_response(status, headers, exc_info) print >>sys.stderr, 'after start_response' return start_response(status, headers, exc_info) print >>sys.stderr, 'before application' start_time = time.time() # Call the wrapped application app_iter = self.application(environ, _start_response) # Do something to modify the response body end_time = time.time() print >>sys.stderr, 'after application' if hasattr(current, 'request'): request = current.request print >>sys.stderr, "%s/%s/%s took %.3f ms to run" % ( request.application, request.controller, request.function , (end_time - start_time) * 1000) # Return modified response return app_iter and to run with gevent, for example: #!/usr/bin/python # coding: utf-8 # To use a virtualenv: #VENV_PATH = '/home/envs/someenv' # #activate_this = VENV_PATH + '/bin/activate_this.py' #execfile(activate_this, dict(__file__=activate_this)) import gevent.monkey gevent.monkey.patch_all() import sys from gevent.pywsgi import WSGIServer # change these parameters as required LOGGING = False SOFTCRON = False import sys import os path = os.path.dirname(os.path.abspath(__file__)) os.chdir(path) if not os.path.isdir('applications'): raise RuntimeError('Running from the wrong folder') sys.path = [path] + [p for p in sys.path if not p == path] sys.stdout = sys.stderr import gluon.main from logger_middleware import Logger if LOGGING: application = Logger(gluon.main.appfactory(wsgiapp=gluon.main.wsgibase, logfilename='httpserver.log', profiler_dir=None)) else: application = Logger(gluon.main.wsgibase) if SOFTCRON: from gluon.settings import global_settings global_settings.web2py_crontype = 'soft' if __name__ == '__main__': print 'Serving on 8088...' WSGIServer(('', 8088), application).serve_forever() Also you can search for logger middlewares, you have one here: https://pypi.python.org/pypi/wsgi-request-logger Ricardo -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.