Two similar logging programs but different ouputs

Disc Magnet discmagnet at
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> wrote:
> On Apr 18, 10:11 pm, Disc Magnet <discmag... at> 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 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:

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, is not explicitly mentioned in the file
configuration. As per what you said, should be disabled.
However, I get the following output:

root WARNING warning 1
foo WARNING warning 2
foo WARNING warning 2 WARNING warning 3 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('')

# Configure root loggers

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


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







format=%(name)s %(levelname)s %(message)s


More information about the Python-list mailing list