Thursday, March 10, 2011

Redirecting log output to sys.stdout on tests

Use the following code inside your tests/ 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')
handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter("%(asctime)s %(levelname)s %(name)s %(message)s")

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.