D
Disc Magnet
I have tried writing two programs which are doing similar activities.
The little difference between the two programs is that the first one
configures logger1 using addHandler() method while the second program
configures logger1 from log.conf file.
However, the output differs for both. The first program displays
warnings from both logger1 and logger2. The second program displays
warning from logger1 only. It does not display the warning from
logger2.
Could you please help me understand this difference? Programs and
log.conf file follow:
#!/usr/bin/env python2.7
# This program prints both the warnings
import logging
# Create loggers
logger1 = logging.getLogger()
logger2 = logging.getLogger('foo.bar')
# Configure both loggers
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(name)s %(levelname)s %(message)s'))
logger1.addHandler(handler)
logger2.addHandler(logging.NullHandler())
# Use both loggers
logger1.warn('warning 1')
logger2.warn('warning 2')
#----------------------------------------------------------------------
#!/usr/bin/env python2.7
# This program prints only the first warning
import logging
import logging.config
# Create loggers
logger1 = logging.getLogger()
logger2 = logging.getLogger('foo.bar')
# Configure root loggers
logging.config.fileConfig('log.conf')
logger2.addHandler(logging.NullHandler())
# Use both loggers
logger1.warn('warning 1')
logger2.warn('warning 2')
#-----------------------------------------------------------------------
"""The file 'log.conf' is as follows:
[loggers]
keys=root
[handlers]
keys=streamHandler
[formatters]
keys=simpleFormatter
[logger_root]
handlers=streamHandler
[handler_streamHandler]
class=StreamHandler
formatter=simpleFormatter
args=(sys.stdout,)
[formatter_simpleFormatter]
format=%(name)s %(levelname)s %(message)s
"""
#-----------------------------------------------------------------------
The little difference between the two programs is that the first one
configures logger1 using addHandler() method while the second program
configures logger1 from log.conf file.
However, the output differs for both. The first program displays
warnings from both logger1 and logger2. The second program displays
warning from logger1 only. It does not display the warning from
logger2.
Could you please help me understand this difference? Programs and
log.conf file follow:
#!/usr/bin/env python2.7
# This program prints both the warnings
import logging
# Create loggers
logger1 = logging.getLogger()
logger2 = logging.getLogger('foo.bar')
# Configure both loggers
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(name)s %(levelname)s %(message)s'))
logger1.addHandler(handler)
logger2.addHandler(logging.NullHandler())
# Use both loggers
logger1.warn('warning 1')
logger2.warn('warning 2')
#----------------------------------------------------------------------
#!/usr/bin/env python2.7
# This program prints only the first warning
import logging
import logging.config
# Create loggers
logger1 = logging.getLogger()
logger2 = logging.getLogger('foo.bar')
# Configure root loggers
logging.config.fileConfig('log.conf')
logger2.addHandler(logging.NullHandler())
# Use both loggers
logger1.warn('warning 1')
logger2.warn('warning 2')
#-----------------------------------------------------------------------
"""The file 'log.conf' is as follows:
[loggers]
keys=root
[handlers]
keys=streamHandler
[formatters]
keys=simpleFormatter
[logger_root]
handlers=streamHandler
[handler_streamHandler]
class=StreamHandler
formatter=simpleFormatter
args=(sys.stdout,)
[formatter_simpleFormatter]
format=%(name)s %(levelname)s %(message)s
"""
#-----------------------------------------------------------------------