Use logging print the output of pprint

yee379 picture yee379 · Jun 19, 2012 · Viewed 37.6k times · Source

I want to use pprint's output to show a complex data structure, but I would like to output it using the logging module rather than stdout.

ds = [{'hello': 'there'}]
logging.debug( pprint.pprint(ds) ) # outputs as STDOUT

Answer

robert picture robert · Jun 19, 2012

Use pprint.pformat to get a string, and then send it to your logging framework.

from pprint import pformat
ds = [{'hello': 'there'}]
logging.debug(pformat(ds))