In 'models' folder, i create the file named '0.py' like this :
def _init_log():
import os, logging, logging.handlers
logger = logging.getLogger(request.application)
logger.setLevel(logging.DEBUG)
handler =
logging.handlers.RotatingFileHandler(os.path.join(request.folder,'static','applog.txt'),'a',
1024*1024,1)
handler.setLevel(logging.DEBUG)
handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s %
(filename)s:%(lineno)d %(funcName)s: %(message)s"))
logger.addHandler(handler)
return logger
app_logging = cache.ram('app_wide_log', lambda:_init_log(),
time_expire=None)
and now in my application, i can write this :
app_logging.info("My message")
or
app_logging.debug("x = %d" % myvar)
Et voilà !
On 27 avr, 18:31, knitatoms <[email protected]> wrote:
> I'd be interested in a step by step guide to setting up this kind of
> logging. I'm new to web2py and python and I think it would help my
> learning.
>
> I already get some good info by monitoring the http log file with the
> free Kiwi log viewer (on Windows) which tails the file.
>
> Could someone explain step by step how to set up the Python logging
> module to write to a file from a web2py application. Thanks!
>
> On Apr 17, 8:37 pm, Keith Edmunds <[email protected]> wrote:
>
>
>
> > What are others doing for application logging? I'm not referring to the
> > HTTP logs, but program-generated logs.
>
> > I've recently been looking at the Python 'logging' module, which I've not
> > used before. I initiate logging from a file in the 'modules' directory
> > which does this:
>
> > ---------------------------------------------------------------------------
> > -----
> > import logging
> > from logging.handlers import SysLogHandler
>
> > logger = logging.getLogger("MyApp")
> > logger.setLevel(logging.DEBUG)
> > hdlr = SysLogHandler(address='/dev/log')
> > formatter = logging.Formatter('tigerpy[%(process)d]: %(levelname)s:
> > %(filename)s at line %(lineno)d: %(message)s') hdlr.setFormatter(formatter)
> > logger.addHandler(hdlr)
> > ---------------------------------------------------------------------------
> > -----
>
> > Then each file that I want to log from does this:
>
> > ---------------------------------------------------------------------------
> > -----
> > import logging
>
> > logger = logging.getLogger("MyApp")
> > .
> > .
> > logger.info("Something interesting happened")
> > ---------------------------------------------------------------------------
> > -----
>
> > This logs to syslog: the only problem is that each logging message is
> > written to syslog many (20-40) times(!). This isn't just in one place in
> > my code, but throughout. I don't think the code is being executed multiple
> > times, so I'm not sure why I'm getting so many log entries.
>
> > So, I'm interested in what others do to log application events.
>
> > --
> > Subscription settings:http://groups.google.com/group/web2py/subscribe?hl=en