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* 'import logging' 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.
'''

import logging

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)

--~--~---------~--~----~------------~-------~--~----~
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