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
On Jun 5, 4:36 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
> Sorry Hans, I still do not understand the problem.
>
> code outside function is executed when a function in the controller is
> called (whatever the function) code in the function is executed only
> when that function is called. A call to redirect causes the browser to
> make another request thus the controller is executed again.
>
> Massimo
>
> On Jun 5, 8:08 am, Hans <johann.scheibelho...@easytouch-edv.com>
> wrote:
>
> > Correction: redirect() usage is NOT causing the problem. Every call of
> > a controller function also executes
> > 1) general parts of the model (db.py)
> > 2) general parts of the controller (default.py)
> > 3) the requested function in controller
>
> > In my case every click to an menu function adds another multiple for
> > log entries.
>
> > Thinking about the response.menu example which gives you different
> > menu options depending on the user being logged in or not I'm
> > convinced that the general controller sections are executed before
> > each requested function. That's good for the dynamically changing menu
> > and other needs.
>
> > For a log file function which should be available for the entire
> > controller I need to make sure its executed only once. Is there a
> > simple way to do that or a different way to achieve a log file
> > function (similar like my example up in this thread) available for the
> > entire controller?
>
> > Thanks,
> > Hans
> > On Jun 5, 2:34 pm, Hans <johann.scheibelho...@easytouch-edv.com>
> > wrote:
>
> > > yes confirmed, the controller is called multiple times. the reason I
> > > found is that the redirect() statements I use cause this. every
> > > redirect() seems to call the controller, not only the redirection
> > > target function.
>
> > > for example my index is mainly a redirect function, based on settings
> > > its for instance doing
> > >  redirect(URL(r=request,f='f1'))
>
> > > then also in other functions I use redirects e.g. based on rights/
> > > settings.
>
> > > I'm using 1.63.5 on WinXP SP3.
>
> > > Is there a better function than redirect() for avoiding to call the
> > > general controller stuff multiple times?
> > > Is redirect() supposed to also call the general controller stuff or
> > > only directly the redirection target function ?
>
> > > Thanks,
> > > Hans
> > > On Jun 4, 10:48 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
>
> > > > Odd I think your controller is being called 3 times. Is that possible?
> > > > I would add a print statement to check.
>
> > > > On Jun 4, 2:29 pm, Hans <johann.scheibelho...@easytouch-edv.com>
> > > > wrote:
>
> > > > > I tried to define a global logging for a web2py app. The problem with
> > > > > that is that if I place the following code in my default.py controller
> > > > > or db.py model then in both cases its executed more than
> > > > > once...resulting into having every log line written multiple times
> > > > > into the log file.
>
> > > > > either starting the default.py or db.py with those lines is yielding
> > > > > the same result
> > > > >     import logging, logging.handlers
>
> > > > >     # Make a global logging object.
> > > > >     lox = logging.getLogger("log")
> > > > >     lox.setLevel(logging.DEBUG)
>
> > > > >     # This handler writes everything to a file.
> > > > >     h1 = logging.FileHandler("/var/log/myapp.log")
> > > > >     f = logging.Formatter("%(levelname)s %(asctime)s %(funcName)s %
> > > > > (lineno)d %(message)s")
> > > > >     h1.setFormatter(f)
> > > > >     h1.setLevel(logging.DEBUG)
> > > > >     lox.addHandler(h1)
>
> > > > >     # This handler emails me anything that is an error or worse.
> > > > >     h2 = logging.handlers.SMTPHandler('localhost', '....@test.com',
> > > > > ['tobenotif...@test.com'], 'ERROR log')
> > > > >     h2.setLevel(logging.ERROR)
> > > > >     h2.setFormatter(f)
> > > > >     lox.addHandler(h2)
>
> > > > > log usage example in controller...
> > > > >     def index():
> > > > >         log = logging.getLogger("log")
> > > > >         log.debug("starting...")
> > > > >         ....do something
> > > > >         log.debug("finishing...")
> > > > >         return()
>
> > > > > log file looks like:
> > > > > DEBUG 2009-06-04 20:27:45,617 index 1 starting...
> > > > > DEBUG 2009-06-04 20:27:45,617 index 1 starting...
> > > > > DEBUG 2009-06-04 20:27:45,617 index 1 starting...
> > > > > DEBUG 2009-06-04 20:27:50,617 index 1 finishing...
> > > > > DEBUG 2009-06-04 20:27:50,617 index 1 finishing...
> > > > > DEBUG 2009-06-04 20:27:50,617 index 1 finishing...
>
> > > > > how can I fix the 'multiple problem' ?
>
>
--~--~---------~--~----~------------~-------~--~----~
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