Logging module

54 views
Skip to first unread message

Roar

unread,
Sep 28, 2009, 8:32:38 AM9/28/09
to web2py-users
I've added the following to the default.py controller:

import logging
LOG_FILENAME = '/tmp/log/log.txt'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG,)

logging.debug('This message should go to the log file, but it does
not')

...but nothing is being logged. Any ideas?

mdipierro

unread,
Sep 28, 2009, 9:13:58 AM9/28/09
to web2py-users
Some time ago Iceberg suggesting adding the following model for
logging:

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)

Richard

unread,
Sep 28, 2009, 6:53:31 PM9/28/09
to web2py-users
I have found that logging is effected by the server you are using. For
the builtin web2py server my logging works, but for Apache with WSGI
the logging gets redirected to standard error.
Is the logging being redirected to your server error/output log files?

Richard

Thadeus Burgess

unread,
Sep 28, 2009, 9:43:22 PM9/28/09
to web...@googlegroups.com
That is one of the problems when using WSGI, all output to STDOUT is used for the server response and STDIN is used for the application input

To get arround this, output to files must be directed to STDERR, otherwise you will mess up your servers response string.

sys.stdout = sys.stderr

http://blog.dscpl.com.au/2009/04/wsgi-and-printing-to-standard-output.html

-Thadeus
Message has been deleted

Roar

unread,
Sep 30, 2009, 7:53:59 AM9/30/09
to web2py-users
Thanks! :)

Iceberg

unread,
Sep 30, 2009, 10:36:19 AM9/30/09
to web2py-users
For your information. There is a refined version later. (And that's
why I suggest add it into scaffold welcome app, this way people can
easily get a latest version.)

The new version uses RotatingFileHandler so that you need not worry
the log file grows up unlimitedly, and the log file can be viewed via
WEB even without writing an action for it. Yet it does not support
GAE. But it does not harm anyway if you just put it in yourapp/models
but not using it.

# This model file defines some magic to implement app_wide_log.
def _init_log(): # Does not work on GAE
import os,logging,logging.handlers
logger = logging.getLogger(request.application)
logger.setLevel(logging.DEBUG)
handler = logging.handlers.RotatingFileHandler(
os.path.join( # so that it can be served as http://.../yourapp/static/applog.txt
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)
Reply all
Reply to author
Forward
0 new messages