views:

90

answers:

2

I need some help understanding what's happening here. This code is from a models/log.py module in web2py, and is meant to allow for global logging.

def _init_log(): 
    logger=logging.getLogger(request.application) 
    ...
    return logger 

logging=cache.ram('mylog',lambda:_init_log(),time_expire=99999999)

Can someone explain how this might work, and what the last line is doing?

Thanks--

+1  A: 

What it does, I think, is that the logging function is "memoized". That means that if it is called with the same arguments multiple times in a row, it will return the old results from its cache.

It might be based on the plone.memoize module, but I couldn't check because the link doesn't work for me.

DiggyF
Thanks Siggy- yeah this might be too specific to web2py's caching mechanism to belong here, probably does mimic the plone module you refer to..
Yarin
+2  A: 

This is not a standard web2py file. Sombody wrote it but I can see what it does: In web2py a single installation can run multiple apps. Some users want different app running under the same web2py to have separate logs, therefore they need different logger objects. In web2py there are not global settings and all user code is executed on every request so in order to avoid re-creating the logger at every request, the logger objects is created only ones and stores in cache with large expiration time 999...9. When an http request arrives, if it need to log, it finds the logger in cache. Look at the docs for cache.ram.

I have used this trick but never for logging.

mdipierro
Thanks Massimo- I've brought this into the web2py users group. Can you take a look?http://groups.google.com/group/web2py/browse_thread/thread/d3b534113a904cd7
Yarin
@massimo- Can you explain how it's assigning something to 'logging'- isn't logging a module?
Yarin