Important note: do *not* 'import logging' in your controller if you
use log.py. its done for all your controllers already in the log.py
model and it would break the log.py magic.

On Jun 15, 7:46 pm, Hans <johann.scheibelho...@easytouch-edv.com>
wrote:
> Log file usage in Web2py is now simpler than ever!
> You want to monitor the activities of your cron-driven web2py
> functions?
>
> To use the per application log file with rotating file handler in
> web2py you need to put log.py (below) into your models folder. The log
> file itself is created in your_web2py_path/applications/
> your_application/app.log
>
> Writing into the log file from your controller works as follows:
> def my_function():
>     logging.debug('my function abc is starting')
>     logging.error('huston we got a %s problem.' % 'major')
>     return ()
>
> Viewing the log file through your application works as follows:
> def show_log():
>     return get_log()
>
> If required the GAE solution needs work.
> A BIG thank you to Iceberg!
> Feedback and usage is welcome ;-)
>
> Hans
>
> Here is the log.py model version I use to enable logging for web2py
> apps.
>
> import logging
>
> def _init_log(level=logging.DEBUG,formatter="%(asctime)s %(levelname)s
> %(funcName)s():%(lineno)d %(message)
> s",filename='app.log',maxBytes=1024*1024,backupCount=2):
>     import os,logging.handlers
>     logger=logging.getLogger(request.application)
>     logger.setLevel(level)
>     if request.env.web2py_runtime_gae: # if running on Google App
> Engine
>         handler=logging.handlers.HTTPHandler(request.env.http_host,URL
> (r=request,f='log')) # assuming there is an optional log action
>     else:
>         handler=logging.handlers.RotatingFileHandler(os.path.join
> (request.folder,filename),maxBytes=maxBytes,backupCount=backupCount)
>     handler.setLevel(level)
>     handler.setFormatter(logging.Formatter(formatter))
>     logger.addHandler(handler)
>     logger.debug("web2py application %s starting" %
> request.application)
>     return logger
>
> def get_log():
>     try:
>         f = open(logging.handlers[0].baseFilename, 'r')
>         c = f.readlines()
>         f.close()
>         return {'log':TABLE(*[TR(str(item)) for item in c])}
>     except:
>         return ()
>
> logging=cache.ram('mylog',lambda:_init_log(),time_expire=99999999)
>
> On Jun 11, 10:20 am, Iceberg <iceb...@21cn.com> wrote:
>
> > Not at all.
>
> > The cache.ram in log.py is to make sure the logger is inited only
> > once, otherwise we will see more and more duplicate log appearing in
> > the log file as Hans mentioned in his earlier posts. The only downside
> > is that you need to restart web2py if you change (but usually you
> > don't need to) log definition.
>
> > As to the "OPTIONAL in controller" part, you can ignore it. They are
> > just a rough demo and not necessary.
>
> > On Jun11, 3:17am, mdipierro <mdipie...@cs.depaul.edu> wrote:
>
> > > Why do you need to cache the logging module? Is this only for speed?
>
> > > On Jun 10, 1:44 pm, Iceberg <iceb...@21cn.com> wrote:
>
> > > > After changing from FileHandler to RotatingFileHandler, now I think
> > > > the per-app log can be turned on by default (otherwise new comers
> > > > won't notice this good feature).
>
> > > > The only thing I am still not sure is whether one can use logging and
> > > > its FileHander (or RotatingFileHander) on GAE's readonly filesystem.
> > > > Tests are welcome.
>
> > > > Anyway, this is my latest code. Maybe it can help you.
>
> > > >   import logging
> > > >   def _init_log(level=logging.DEBUG):
> > > >     import os,logging.handlers
> > > >     logger=logging.getLogger(request.application)
> > > >     logger.setLevel(level)
> > > >     if request.env.web2py_runtime_gae: # if running on Google App
> > > > Engine
> > > >       handler=logging.handlers.HTTPHandler(
> > > >         request.env.http_host,URL(r=request,f='log')) # assuming there
> > > > is an optional log action
> > > >     else:
> > > >       handler=logging.handlers.RotatingFileHandler(
> > > >         os.path.join
> > > > (request.folder,'app.log'),maxBytes=1024*1024,backupCount=2)
> > > >     handler.setLevel(level)
> > > >     handler.setFormatter(logging.Formatter(
> > > >       "%(asctime)s %(levelname)s %(funcName)s():%(lineno)d %(message)
> > > > s"))
> > > >     logger.addHandler(handler)
> > > >     return logger
> > > >   logging=cache.ram('mylog',lambda:_init_log(),time_expire=99999999)
>
> > > > OPTIONAL in controller:
>
> > > > def log():
> > > >   if request.vars: # append
> > > >     history=cache.ram('log_in_cache',lambda:[],time_expire=99999999)
> > > >     history.append(request.vars)
> > > >     cache.ram('log_in_cache',lambda h=history:h,time_expire=0)#set
> > > >   else: # show
> > > >     return {'':TABLE(*[TR(item) for item in
> > > >       cache.ram('log_in_cache',lambda:None,time_expire=99999999)])}
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py Web Framework" group.
To post to this group, send email to web2py@googlegroups.com
To unsubscribe from this group, send email to 
web2py+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to