I'm trying to redirect some logging output.
import sys
import logging
from contextlib import stdout
logging.basicConfig(stream=sys.stdout)
logger = logging.getLogger()
with open("out.txt", "w") as f:
with redirect_stdout(f):
print("STDOUT")
logger.warning("LOGGER")
I would expect both of these to output to out.txt
but instead STDOUT
goes to the file, and WARNING:root:LOGGER
displays in the terminal.
I've confirmed that that logger is outputting to stdout
by removing the redirect stuff and running python script.py > stdout.txt
. Then everything gets to the file, as it should.
So the logger
must be doing something weird that causes it to not get pick up by redirect_stdout
?
(Note: The behaviour is the same with redirect_stderr
).
