get_current_request() for debug output / logging?

20 views
Skip to first unread message

Achim Domma

unread,
Jul 20, 2014, 5:01:01 AM7/20/14
to pylons-...@googlegroups.com
Hi,

the documentation is quite clear about the "Don't do that!!!" regarding usage of get_current_request(). But however I approach my problem, I end up with get_current_request being the best solution, so I thought I should ask for permission. ;-)

Here is my scenario:

I have some service object which is registered in my application registry and requested by my application code from there. The object is independent of Pyramid, can be used and tested in isolation, so everything's fine. Deep inside the service, I fire some events to allow logging of some debug information (generated http requests and so on). Now I would like to display this information in the debug toolbar, of course on a per request base. Obviously passing the request to my service object is not an option as it would break the decoupling. Passing some logging handler through the whole service API is not nice too. Actually that's what events are made for. So the event handler needs to know about the current request. Whatever solution I could think of, I would have to implement something like a thread local. And then I could also use the existing get_current_request.

Does anybody see a better solution?

cheers,
Achim

Jonathan Vanasco

unread,
Jul 21, 2014, 12:01:45 PM7/21/14
to pylons-...@googlegroups.com
The docs would suggest that you're fine for this use case


     This function should be used extremely sparingly, usually only in unit testing code. It's almost always usually a mistake to use get_current_request outside a testing context because its usage makes it possible to write code that can be neither easily tested nor scripted.

That being said...

Have you considered this setup--

`add_request_method` a function that grabs the service object from the registry and reifies it into the request as a property.  when you do that, you could create a new instance of the service object and have that instantiated with  kwarg for the request object / request id.   your code then addresses the per-request object as a property of the request, not the original registry object used as a template.
Reply all
Reply to author
Forward
0 new messages