Thursday, March 10, 2011

Redirecting log output to sys.stdout on tests

Use the following code inside your tests/base.py file if you need to redirect the log output to sys.stdout for debugging purposes:

import logging
import sys

logger = logging.getLogger('YOUR LOGGER NAME')
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter("%(asctime)s %(levelname)s %(name)s %(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)

This way you can see debug(), info(), warning(), error() and critical() messages printed in the console.

More information: Logging facility for Python and Logging on Plone.