Documenting all HTTP requests to an application

18 views
Skip to first unread message

Alec Munro

unread,
May 10, 2011, 2:40:12 PM5/10/11
to pylons-devel
Hi list,

I want to create a map of all the requests to an application, to make
it easier to refactor it without breaking it's interfaces. What's the
easiest place to add a function to be called for every request?

Thanks,
Alec

Jasper van den Bosch

unread,
May 10, 2011, 2:51:22 PM5/10/11
to pylons...@googlegroups.com
If you use traversal, perhaps in the constructor of your Root resource?


--
You received this message because you are subscribed to the Google Groups "pylons-devel" group.
To post to this group, send email to pylons...@googlegroups.com.
To unsubscribe from this group, send email to pylons-devel...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pylons-devel?hl=en.


Alec Munro

unread,
May 10, 2011, 2:59:16 PM5/10/11
to pylons-devel
D'oh!

I should have specified that I'm using Pylons 0.96 for this particular
application.

Thanks,
Alec

On May 10, 3:51 pm, Jasper van den Bosch <jap...@gmail.com> wrote:
> If you use traversal, perhaps in the constructor of your Root resource?
>

Alice Bevan–McGregor

unread,
May 10, 2011, 9:26:41 PM5/10/11
to pylons...@googlegroups.com
Create a small bit of middleware to log the environ['REQUEST_URI'] (or
other value, as appropriate) of each GET. You can then direct that
logger to output to a file, another handler (i.e. you could write one
which logs to your database of choice and de-duplicates), etc.

Logging to a file then running 'sort -u' across the resultant file will
give you all of the unique addresses visited by users during the
logging period.

— Alice.

Alec Munro

unread,
May 11, 2011, 10:32:38 AM5/11/11
to pylons-devel
Thanks Alice. I wasn't sure how to insert/structure middleware, but
this page helped:
http://wiki.pylonshq.com/display/pylonscookbook/Adding+your+own+middleware

My solution looks roughly like this:

class RequestLogger(object):
"""WSGI middleware to log all requests."""

def __init__(self, app):
self.app = app
self.uris = set()

def __call__(self, environ, start_response):
self.uris.add(environ['PATH_INFO'] + "\n")
open("request_log.txt", "w").writelines(self.uris)
return self.app(environ, start_response)

With it being registered at the end of middleware.py:

return RequestLogger(app)

Alec

P.S. I know my implementation isn't ideal, but I'm working on that. ;)

Daniel Holth

unread,
May 11, 2011, 10:52:47 AM5/11/11
to pylons...@googlegroups.com
You don't log requests already? Here is one: http://pythonpaste.org/modules/translogger.html

Perhaps the format could just log the path without having to do any text processing on the file.

I thought your original question was more like 'print out all the routes' without making requests to the application?

Alec Munro

unread,
May 12, 2011, 10:34:57 AM5/12/11
to pylons-devel
Nope, haven't needed it yet. This case is a bit special because I want
unique routes, but I want to filter out query string values (and ids
in urls).

I think you have my original question more or less correct. I want to
know how the app is being used, so when I rewrite it using Pyramid, I
can make sure it still does all that. :)
Reply all
Reply to author
Forward
0 new messages