multiple logger

peiyin.li at gmail.com peiyin.li at gmail.com
Fri Oct 7 21:27:10 CEST 2005


Hi,

I want to have two loggers that logs to two different files. Here is
what I have:

import logging
import logging.handlers

project1Handler = logging.handlers.RotatingFileHandler( 'project1.log',
maxBytes=1024)
project1Handler.setLevel(logging.INFO)
formatter1 = logging.Formatter('%(name)-12s: %(levelname)-8s
%(message)s')
project1Handler.setFormatter(formatter1)
logging.getLogger('project1').addHandler(project1Handler)
logger1 = logging.getLogger('project1')

logger1.debug('Quick zephyrs blow, vexing daft Jim.')
logger1.info('How quickly daft jumping zebras vex.')
logger1.warning('Jail zesty vixen who grabbed pay from quack.')
logger1.error('The five boxing wizards jump quickly.')


project2Handler = logging.handlers.RotatingFileHandler( 'project2.log',
maxBytes=1024)
project2Handler.setLevel(logging.DEBUG)
formatter2 = logging.Formatter('%(name)-12s: %(levelname)-8s
%(message)s')
project2Handler.setFormatter(formatter2)
logging.getLogger('project2').addHandler(project2Handler)
logger2 = logging.getLogger('project2')

logger2.debug('Quick zephyrs blow, vexing daft Jim.')
logger2.info('How quickly daft jumping zebras vex.')
logger2.warning('Jail zesty vixen who grabbed pay from quack.')
logger2.error('The five boxing wizards jump quickly.')


However, I got this for project1.log:

project1    : WARNING  Jail zesty vixen who grabbed pay from quack.
project1    : ERROR    The five boxing wizards jump quickly.

and this for project2.log:

project2    : WARNING  Jail zesty vixen who grabbed pay from quack.
project2    : ERROR    The five boxing wizards jump quickly.

Where are the DEBUG and INFO messages go?

I then add this at the begining of my code:

logging.basicConfig(level=logging.DEBUG)

Now it seems that both project1.log and project2.log has the correct
output. But I also see all the log message going to the console, which
I really don't want.

What did I miss here? Do I have to set a handler for the root logger
before I can set something for the child loggers? If yes, how do I make
the root logger logs to nowhere?

Thanks in advance!

Albert




More information about the Python-list mailing list