Two similar logging programs but different ouputs

Disc Magnet discmagnet at gmail.com
Tue Apr 19 01:35:19 EDT 2011


Thank you Vinay for the quick reply. I have a few more questions.

On Tue, Apr 19, 2011 at 3:27 AM, Vinay Sajip <vinay_sajip at yahoo.co.uk> wrote:
> On Apr 18, 10:11 pm, Disc Magnet <discmag... at gmail.com> wrote:
>
>> Could you please help me understand this difference? Programs and
>> log.conf file follow:
>
> The first program prints two messages because loggers pass events to
> handlers attached to themselves and their ancestors. Hence, logger1's
> message is printed by logger1's handler, and logger2's message is
> printed by logger1's handler because logger1 is an ancestor of
> logger2.
>
> In the second case, logger foo.bar exists when fileConfig() is
> called,  but it is not named explicitly in the configuration. Hence,
> it is disabled (as documented). Hence only logger1's message is
> printed.

I couldn't find this mentioned in the documentation at:

http://docs.python.org/library/logging.config.html#configuration-file-format

Could you please tell me where this is documented?

>
> NullHandler is a handler which does nothing - there is no point in
> adding it to a system which configures logging, and only any point in
> adding it to top-level loggers of libraries which may be used when
> logging is not configured by the using application (this is also
> documented).
>

Actually, I was learning how logging works with NullHandler because I
am going to use it with a library I am writing. The code that I shared
is meant for experimenting. Once, I am comfortable with the logging
API, I'll separate the code into different modules.

In the following code, foo.bar is not explicitly mentioned in the file
configuration. As per what you said, foo.bar should be disabled.
However, I get the following output:

root WARNING warning 1
foo WARNING warning 2
foo WARNING warning 2
foo.bar WARNING warning 3
foo.bar WARNING warning 3

Program and configuration file follow:

#!/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')
logger3 = logging.getLogger('foo.bar')

# Configure root loggers
logging.config.fileConfig('log.conf')
logger2.addHandler(logging.NullHandler())
logger3.addHandler(logging.NullHandler())

# Use both loggers
logger1.warn('warning 1')
logger2.warn('warning 2')
logger3.warn('warning 3')

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

"""The file 'log.conf' is as follows:

[loggers]
keys=root,foo

[handlers]
keys=streamHandler

[formatters]
keys=simpleFormatter

[logger_root]
handlers=streamHandler

[logger_foo]
qualname=foo
handlers=streamHandler

[handler_streamHandler]
class=StreamHandler
formatter=simpleFormatter
args=(sys.stdout,)

[formatter_simpleFormatter]
format=%(name)s %(levelname)s %(message)s
"""

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



More information about the Python-list mailing list