When you import tensorflow, it prints to stderr
- bug report.
Currently this is confusing my monitoring system and logging these informative messages as errors.
I would like to redirect these messages from stderr
to stdout
.
In theory, this should work:
def redirect_tensorflow_logs():
print("Before - Outside redirect block", file=sys.stderr)
with redirect_stderr(sys.stdout):
print("Before - Inside redirect block", file=sys.stderr)
import tensorflow as tf
print("After - Inside redirect block", file=sys.stderr)
print("After - Outside redirect block", file=sys.stderr)
Unfortunately, it is not. This is the output I am getting:
Output - stderr:
Before - Outside redirect block
2024-01-10 14:34:44.164579: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
After - Outside redirect block
Output - stdout:
Before - Inside redirect block
After - Inside redirect block
Is there a way to redirect these messages from stderr
to stdout
?
I know that set os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
(https://stackoverflow.com/a/42121886/251589) to remove these messages entirely. I would prefer to not do this because at some point, I will want to address these and hiding them will make this harder for future devs to understand what is going on.
I could also redirect ALL output from my program from stderr to stdout via something like this: python -m myprogram 2>&1
. This would stop my monitoring system from raising errors but may cause me to miss some items in the future that are real errors.
The best solution that I have found for this is to map stderr to stdout.
This is slightly tricky because you need to capture the stderr from the underlying c-process that tensorflow kicks off.
This works:
import sys
from wurlitzer import pipes
with pipes(stdout=None, stderr=sys.stdout):
import tensorflow # noqa: W0612