views:

74

answers:

2

In our Pylons based web-app, we're creating a class that essentially provides some logging functionality. We need a new instance of this class for each http request that comes in, but only one per request.

What is the proper way to go about this? Should we just create the object in middleware and store in in request.environ? Is there a more appropriate way to go about this?

+1  A: 

There's a good implementation of request local variables in Paste: paste.registry Pylons uses this for its own request-local global variables.

Just create the object in "middleware" like documented and import the global variable to the modules where you need it. (middleware in scarequotes because it's not strictly middleware, because you depend on it to function it's a part of your application/framework)

Ants Aasma
A: 

May be to rely on the built-in functionality?

import logging
logging.getLogger(__name__)

The logging functionality is rather customizable in Python.

newtover
Yeah, we're actually using that. We're not rewriting the logging functionality itself, we're writing a class that determines what information exactly to log.
dave mankoff
@dave mankoff: and filters, handlers and formatters do not fit your needs, do they?
newtover
@newtover: nope. its not about filtering the available info, its about capturing the information specific to our app and making it available to the logger.
dave mankoff