Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Log everything from every module except one third party module

I have a main.py:

import logging, sys
logging.basicConfig(
    level=logging.DEBUG, 
    handlers=[logging.FileHandler("test.log"), logging.StreamHandler(sys.stdout)]
)  # log everything to file and stdout
logger = logging.getLogger("main")
logger.info("hello main!")

import thirdparty_module
thirdpary_module.foo()

import my_submodule1

which imports my_submodule1, my_submodule2my_submodule9 that log a few things in DEBUG:

import logging 
logger = logging.getLogger("my_submodule1")
logger.debug("my_submodule1 debug test")

and a third party module thirdparty_module.py which I don’t control, but that logs too many things in DEBUG:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import logging, time, threading
logger = logging.getLogger("thirdparty_module")

def foo():
    threading.Thread(target=bar).start()

def bar():
    while True:
        logger.debug(f"thirdparty_module {time.time()}")    # flood!!
        time.sleep(0.1)

Question: how to keep DEBUG logging "ON" for main.py, my_submodule1.py, …, my_submodule9.py, but avoid thirdparty to log anything in DEBUG?

Note:

>Solution :

If the example is correct, then you can get thirdparty.logger. Logger objects have propagate attribute – this sets whether logs propagate to the upper loggers and their handlers. By default it’s set to True, just switch it to False:

# main.py
...
import thirdparty_module
thirdparty_module.logger.propagate = False
thirdparty_module.foo()
...

Quick test:

import logging
logging.basicConfig(level=logging.DEBUG)

l1 = logging.getLogger()
l2 = logging.getLogger("test")

l2.debug("test") # this prints

l2.propagate = False
l2.debug("test") # this doesn't print
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading