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)])}

On Jun10, 5:52pm, Hans <johann.scheibelho...@easytouch-edv.com> wrote:
> WOW Iceberg! That should also work well for me. Thanks for sharing
> your solution idea! I'm going to try it later today.
> And I agree the 'uncomment me if you need app-logging' approach in
> combination with the per-application-log-file seems also to me the
> best (backward compatible, flexible).
>
> Since I'm going to implement that for me anyway I'll send my code when
> its finised...in case you want to improve it and/or Massimo wants to
> incorporate it in future web2py versions.
>
> On Jun 9, 9:02 pm, Iceberg <iceb...@21cn.com> wrote:
>
> > Mmm, even after this log.py goes into models/log.py, I think the line:
> >   logging=cache.ram(...)
> > should be replaced by:
> >   import logging
> >   # logging=cache.ram(...) # Uncomment me if you need app-logging
>
> > Because:
> > 1. My log.py will cause the an incremental system.log which will
> > eventually cram up your disk, hence it is not good for production
> > site, unless you use RotatingFileHandler instead.
> > 2. On GAE, you can not write to disk anyway, unless you use
> > SMTPHandler or HTTPHandler etc.
>
> > You get the idea. //shrug
>
>
> > > On Jun10, 2:22am, mdipierro <mdipie...@cs.depaul.edu> wrote:
>
> > > > I think you nailed it! I would change the file name so that it resides
> > > > inside the applicaitons folder:
>
> > > > handler=logging.FileHandler(os.path.join(request.folder,"system.log"))
>
> > > > we could also add a line in the admin to peek the log (like with
> > > > sql.log).
>
> > > > On Jun 9, 12:18 pm, Iceberg <iceb...@21cn.com> wrote:
>
> > > > > request.log(message) would be nice, but all the advantages of standard
> > > > > logging are too good to let go. So I came up with this little trick.
>
> > > > > Add following codes inside model/log.py:
>
> > > > >   def _init_log():
> > > > >     import logging
> > > > >     logger=logging.getLogger(request.application)
> > > > >     logger.setLevel(logging.DEBUG)
> > > > >     handler=logging.FileHandler("%s.log"%request.application)
> > > > >     handler.setLevel(logging.DEBUG)
> > > > >     handler.setFormatter(logging.Formatter(
> > > > >       "%(levelname)s %(asctime)s %(funcName)s %(lineno)d %(message)
> > > > > s"))
> > > > >     logger.addHandler(handler)
> > > > >     return logger
> > > > >   logging=cache.ram('once',lambda:_init_log(),time_expire=99999999)
>
> > > > > Then, in any of your controller, you can just do the usual:
> > > > >   logging.warn('blah blah')
> > > > > The only tricky point is you can not do this inside your controller:
> > > > >   import logging
> > > > > otherwise you lose the magic. ;-)
>
> > > > > Besides, thanks for Hans for bringing up all the issue, which inspires
> > > > > me to find the solution (at least for me) for this problem haunting me
> > > > > for long time.
--~--~---------~--~----~------------~-------~--~----~
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