0

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>]
1
  • How did you get the result? Using dir() in which module? If you want testme to be logging from ch in myapp, just pass ch as a function to testme def testme(ch): Commented Jan 22, 2016 at 1:27

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much! I was under the impression the point of entry would always be picked as the root logger my bad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.