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.
>
> On Jun9, 10:35pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
>
> > I agree about 1 and 2. I do not thing logging belongs to cache either.
>
> > i think we want something like
>
> >    request.log(message)
>
> > which does something like.
>
> >    open(os.path.join(request.folder,'app.log'),'w').write(message
> > +'\n')
>
> > Am I wrong? The problem is that this naive approach would bypass OS
> > logging and filtering capabilities supported by logging. we should
> > look into the logging module perhaps we can have a logger per app?
>
> > Massimo
>
> > On Jun 9, 8:32 am, Hans <johann.scheibelho...@easytouch-edv.com>
> > wrote:
>
> > > Sorry for being picky on this. I think the database is not a suitable
> > > place either to store a 'application instance initialized flag'.
> > > Reason:
> > > 1) the db can be used by more than one instance of the app
> > > 2) if the app stops execution without setting back the 'db record' to
> > > 'app not initialized' - like it happens when you remove the power -
> > > then the application will not work any more because the code will
> > > refer to objects which are not created yet because the one-time-
> > > initialization per app was not executed.
>
> > > I'm not aware how application wide vars in web2py cache are working,
> > > but I assume it could be a viable option.
>
> > > what do you think?
>
> > > On Jun 8, 2:43 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
>
> > > > You are right. Not in session. You can store an application wide var
> > > > in cache but I would suggest using the db for this. Anyway, I will
> > > > post an example.
>
> > > > On Jun 8, 4:27 am, Hans <johann.scheibelho...@easytouch-edv.com>
> > > > wrote:
>
> > > > > The log file initialization code must be executed once when the
> > > > > application starts and should not be executed multiple times (e.g. not
> > > > > once per user and not once per controller execution).
>
> > > > > I'm not aware that in a session I can store a application-wide global
> > > > > variable - independent of the user.
>
> > > > > Can you provide a link or example code for execution only once per
> > > > > application (e.g. initialization function) ?
>
> > > > > On Jun 5, 10:19 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
>
> > > > > > can you store it into a session?
>
> > > > > > On Jun 5, 2:11 pm, Hans <johann.scheibelho...@easytouch-edv.com>
> > > > > > wrote:
>
> > > > > > > The problem I have with my code for generating log files that the 
> > > > > > > part
> > > > > > > of the code (see first posting in this thread) which is outside 
> > > > > > > any
> > > > > > > function in the controller (because it should be available for all
> > > > > > > functions in this controller) is executed every time a function is
> > > > > > > called. I have not found a way to execute it only once intitially 
> > > > > > > and
> > > > > > > prevent multiple executions. Hence it registeres log handlers 
> > > > > > > every
> > > > > > > time the controller is executed which causes every log line to be
> > > > > > > written multiple times into the log file.
>
> > > > > > > The following did not work:
>
> > > > > > > try:
> > > > > > >     done_once
> > > > > > > except:
> > > > > > >     place code to be executed only once here
> > > > > > >     global done_once
> > > > > > >     done_once=true
>
> > > > > > > because the variable done_once is undefined each time the 
> > > > > > > controller
> > > > > > > is executed again.
>
> > > > > > > How can a code in the controller be executed only once?
> > > > > > > Also completely different approach suggestions are welcome!
>
> > > > > > > Thanks,
> > > > > > > Hans
--~--~---------~--~----~------------~-------~--~----~
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