tags:

views:

279

answers:

2
+6  A: 

First off, I get a different output on my machine (running Python 2.6):

ROOT
BOTTOM HANDLER
TOP HANDLER
ROOT

Filtering is only applied on the logger that the message is issued to, and if it passes the filters, it's then propagated to all the handlers of the parent loggers (and not the loggers themselves) - I don't know the rationale for this decision. If you want to stop propagation at say the "top" Logger instance, set:

top.propagation = False
oggy
Thanks! This is exactly what I was looking for!
smcq
Correct command is `top.propagate = False` instead of propagation.
Sorin Sbarnea
A: 

Source is messed with different case identifiers, more useful version:

import logging, sys
root = logging.getLogger('')
level1 = logging.getLogger('level1')
level2 = logging.getLogger('level1.level2')

class KillFilter(object):
    def filter(self, msg):
        return 0

root_handler = logging.StreamHandler(sys.stdout)
top_handler = logging.StreamHandler(sys.stdout)
bottom_handler = logging.StreamHandler(sys.stdout)
root_handler.setFormatter(logging.Formatter('ROOT HANDLER - %(msg)s'))
top_handler.setFormatter(logging.Formatter('level1 HANDLER - %(msg)s'))
bottom_handler.setFormatter(logging.Formatter('level2 HANDLER - %(msg)s'))

msg_killer = KillFilter()

root.addHandler(root_handler)
level1.addHandler(top_handler)
level2.addHandler(bottom_handler)

level1.addFilter(msg_killer)
level1.propagate = False

root.error('root_message')
level1.error('level1_message')
level2.error('level2_message')
Denis Barmenkov