Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #3501

Two similar logging programs but different ouputs

Date 2011-04-19 02:41 +0530
Subject Two similar logging programs but different ouputs
From Disc Magnet <discmagnet@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.524.1303161119.9059.python-list@python.org> (permalink)

Show all headers | View raw


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
"""

#-----------------------------------------------------------------------

Back to comp.lang.python | Previous | NextNext in thread | Find similar


Thread

Two similar logging programs but different ouputs Disc Magnet <discmagnet@gmail.com> - 2011-04-19 02:41 +0530
  Re: Two similar logging programs but different ouputs Vinay Sajip <vinay_sajip@yahoo.co.uk> - 2011-04-18 14:55 -0700
  Re: Two similar logging programs but different ouputs Vinay Sajip <vinay_sajip@yahoo.co.uk> - 2011-04-18 14:57 -0700
    Re: Two similar logging programs but different ouputs Disc Magnet <discmagnet@gmail.com> - 2011-04-19 11:05 +0530
      Re: Two similar logging programs but different ouputs Vinay Sajip <vinay_sajip@yahoo.co.uk> - 2011-04-19 00:56 -0700

csiph-web