Hi There!

Sorry to resurrect this long time dead post but I'm really wondering what 
is the proper way to log actions, errors and warns in 
Web2Py. Tried Hans/Iceberg solution but the log file is not created... Read 
in other topic about some logging functionality integrated but found none 
in web2py book. So, any hints?

Thanks!


On Friday, June 26, 2009 6:28:01 PM UTC-3, Hans wrote:
>
> Mike, 
> instead of importing in your Web2py controller use 
> execfile('your_module.py') 
>
> In your module ... 
> try: 
>       # this variable is available in Web2Py 
>       # but raises an exception if run outside Web2Py 
>       # since its not defined 
>         request.env.web2py_version 
>       # put here your web2py related code... 
>          print 'code executed in Web2py %s' % request.env.web2py_version 
> except: 
>         # put here your not Web2Py related code 
>          print 'code NOT executed in Web2py environment' 
>
> #put here your general code 
> print 'this part is always executed' 
>
> Hans 
>
> On Jun 25, 10:40 pm, MikeEllis <[email protected]> wrote: 
> > This looks really useful. Thanks for writing it. I'm trying to import 
> > some existing python modules that have logging calls into a web2py 
> > app.  Just putting log.py into my models directory helps some, but I'm 
> > not seeing any messages from my existing modules with level lower than 
> > logging.ERROR. I am, however, getting lower level (down to 
> > logging.DEBUG) from the web2py framework.  I suspect it has something 
> > to do with the customized logger my modules are using.  I don't want 
> > to go through and comment out all the calls to my version of getLogger 
> > () because many of these modules are used by other code.  I'm 
> > wondering if there's a way for a python module to detect at runtime 
> > whether it's being imported from the web2py framework. I might then be 
> > able to make my customized logging setup do nothing when running under 
> > web2py.  Any suggestions? 
> > 
> > Thanks, 
> > Mike Ellus 
> > 
> > On Jun 25, 8:29 am, Hans <[email protected]> 
> > wrote: 
> > 
> > > Richard, 
> > 
> > > the following solution works for me in production without any 
> > > problems. I use it for periodic, unattended functions (cron). I've 
> > > send Massimo the code in case he wants to include it into  web2py or 
> > > make it available as optional add-on download. 
> > 
> > > Hans 
> > 
> > > ''' 
> > > To use the per application log file with rotating file handler in 
> > > web2py you need to put this file (log.py) 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 ;-) 
> > 
> > > Important note: do *not* 'importlogging' 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. 
> > > ''' 
> > 
> > > importlogging 
> > 
> > > 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) 
> > 
> >

-- 



Reply via email to