开发者

Help with Python code

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--


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜