0

I have a python script and it has a logger logger = logging.getLogger(__name__)

I have imported many modules in the script (for example: kiteconnect). I wish to have a separate logger for all the imported modules. something like import_logger = logging.getLogger(**all_modules_excluding__name__**)

I wish to have separate loggers for the module ( that i am inside) and for the modules ( which I have imported)

How can I do that?

thanks in advance

4
  • The logger created for that module should be left in place and ideally not be reused outside of that module. So what exactly are you trying to do? Commented Nov 27, 2019 at 5:41
  • The imported modules instantiate a logger and are logging messages and you wish to override those loggers? Commented Nov 27, 2019 at 5:41
  • I wish to have separate loggers for the module ( that i am inside) and for the modules ( which I have imported) Commented Nov 27, 2019 at 5:46
  • However you declared the logger for the module you are in, why not do the same for the modules you imported? Commented Nov 27, 2019 at 5:51

1 Answer 1

1

If you do not control the source code of the modules you can't stop them from creating their own loggers that all have different names. There is however one feature that helps you: loggers are hierarchical, and unless propagation hasn't been explicitly turned off on them they all will send their logs up to the root logger. So in effect the root logger will have all the logs from all modules. If you turn of propagation on your app logger you effectively have your app logs on that logger, and all module logs in the root logger. You may still have to get the individual modules loggers to set their level. In code it would look something like this:

import logging #import modules here import_logger = logging.getLogger() import_logger.addHandler(logging.StreamHandler()) # use any handler you want logger = logging.getLogger(__name__) logger.propagate = False 
Sign up to request clarification or add additional context in comments.

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.