0

I have a core python library (over which I have read only access). This core lib has the below statements (note: logging set to NullHandler)

logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())

At places, the core lib, also has statements for logging INFO level messages, such as,

logger.info("In test1")

At some point later,

logger.info("In test2")                

I am trying to pull the logs from the core lib using my python client. In the python client, I add the below statements:

import logging
logger = logging.getLogger('myapp')
hdlr = logging.FileHandler('temp.log')
logger.addHandler(hdlr) 
logger.setLevel(logging.INFO)

But, this would only log the INFO level messages local to my client script.

Que:Is there a way I could tweak my client to pull logs from the core lib?

3 Answers 3

1

You will need to get the logger defined in core.

$ cat core.py
import logging

logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())

def core_fn():
    logger.info("I'm in core..")


$ cat client.py
import logging
import core

if __name__ == '__main__':
    #logger = logging.getLogger('myapp')
    logger = logging.getLogger(__name__)
    hdlr = logging.FileHandler('temp.log')
    logger.addHandler(hdlr)
    logger.setLevel(logging.INFO)
    logger.info("I'm in client")

    logger2 = logging.getLogger('core')
    logger2.addHandler(hdlr)
    logger2.setLevel(logging.INFO)

    core.core_fn()
$ python client.py
$ cat temp.log
I'm in client
I'm in core..
$
Sign up to request clarification or add additional context in comments.

Comments

1

See the discussion here about how the logger names work: https://docs.python.org/2/library/logging.html#logger-objects

Your logging.getLogger('myapp') only sees the library's logs if the library's logger inherits from your 'myapp' logger. Since it's named __name__ I think that will only be true if the library is a submodule of your app (i.e. it's in a subfolder under your app). My understanding of imports could be wrong though; I haven't actually tested what __name__ resolves to in a core module.

In general, you would need to know the string name of the library's logger (whatever __name__ resolves to, in this case.), then call getLogger to grab that. But in your specific case, you can just import the logger object from any module in the library. Then add your handler to it.

Comments

1

The logging module has a manager that keeps track of all existing loggers. You could iterate through it and add your handler to all loggers, like so:

logger = logging.getLogger(__name__)
hdlr = logging.FileHandler("temp.log")


for logr in logging.Logger.manager.loggerDict.values():
    logr.addHandler(hdlr)
    logr.setLevel(logging.INFO)

Of course, this has to be done after the logger of the core module has been created. And it needs the core lib to run in the same process as your client.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.