I'm importing a module foo
which uses Python's logging
module. However, foo
produces a huge amount of logging output, and I need to use stdout
to communicate important information to the user, which is largely being drowned out by the ridiculous output of the module I'm importing.
How can I disable the module's ability to log to stdout
without modifying foo
's code? I still want it to log to the files it logs to, but I don't want it logging to stdout
.
I have tried the following:
logging.getLogger("foo").propagate = False
and
@contextlib.contextmanager
def nostdout():
class DummyFile(object):
def write(self, x): pass
save_stdout = sys.stdout
sys.stdout = DummyFile()
yield
sys.stdout = save_stdout
with nostdout(): import foo