Reputation: 401
I have been working on this almost all day couldn't figure what I am missing. I am trying to add a custom handler to emit all log data into a GUI session. It works but the handler doesn't extend to the submodules and just emits records from the main module. Here is a small snippet I tried
I have two files
# main.py
import logging
import logging_two
def myapp():
logger = logging.getLogger('myapp')
logging.basicConfig()
logger.info('Using myapp')
ch = logging.StreamHandler()
logger.addHandler(ch)
logging_two.testme()
print logger.handlers
myapp()
Second module
#logging_two
import logging
def testme():
logger = logging.getLogger('testme')
logger.info('IN test me')
print logger.handlers
I would expect the logger in logging_two.testme to have the handler I have added in the main module. I looked at the docs to me it seems this should work but I am not sure if I got it wrong?
the result I get is
[]
[<logging.StreamHandler object at 0x00000000024ED240>]
Upvotes: 0
Views: 1820
Reputation: 1458
In myapp()
you are adding the handler to the logger named 'myapp'
. Since testme()
is getting the logger named 'testme'
it does not have the handler since it is a different part of the logging hierarchy.
If you just have logger = logger.getLogger()
in myapp()
then it would work since you are adding the handler to the root of the hierarchy.
Check out the python logging docs.
Upvotes: 1