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_submodule2
... my_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:
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:
I don't want to fork
thirdparty_module.py
to remove thelogging.setLevel(...)
.I have already read Python Logging - Disable logging from imported modules but it didn't fully cover this use case. Update: this answer helped.