Tuesday, July 17, 2012

Logging in python

It's just a wee bit tricky to set up python logging such that each module has its own logger and the logger level is controlled by a main script. The way to do that is illustrated below. The crucial line is

logging.basicConfig(level=logging.DEBUG)

in the main script. This line sets up all the loggers from root onwards and sets them to a uniform level.

main.py

import logging
import module as m
logging.basicConfig(level=logging.DEBUG)

logger = logging.getLogger(__name__)
logger.warning('This is a warning from the main script')
m.f1()


module.py 
import logging
logger = logging.getLogger(__name__)

def f1():
  logger.info('A warning from the module')

No comments:

Post a Comment