Tensorflow causes logging messages to double
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
When TensorFlow log messages appear twice, the problem is usually not TensorFlow "printing twice" by itself. The more common cause is Python logging configuration: multiple handlers, propagation to the root logger, or repeated notebook-cell setup that attaches the same logging output path more than once.
Understand Where the Duplication Comes From
TensorFlow uses Python's logging system on the Python side, and it also emits some C++-level logs. Duplicate Python log lines usually happen because the same message is handled by more than one logger configuration path.
Typical causes include:
- calling
logging.basicConfig(...)multiple times in interactive sessions - adding a stream handler manually without clearing existing handlers
- letting TensorFlow's logger propagate to a root logger that already has a handler
Once more than one handler points to the same console output, the message appears multiple times even though it was emitted only once.
Inspect and Reconfigure the TensorFlow Logger
TensorFlow exposes a standard logger object through tf.get_logger(). That makes it possible to inspect handlers and set a clean configuration explicitly.
This pattern removes existing handlers, adds one fresh stream handler, and disables propagation so the same record is not also sent up to another logger hierarchy.
If you already have a global logging configuration for the whole application, you may prefer to keep propagation enabled and configure only the root logger once. The important part is to avoid configuring both independently in a way that sends one message down two console paths.
Be Careful in Notebooks and Repeated Imports
Jupyter notebooks make this problem more common because cells are executed repeatedly. If a cell adds a handler every time it runs, you silently accumulate duplicates:
If that number keeps growing after rerunning setup cells, your logging duplication is self-inflicted. The fix is to make logger setup idempotent or to clear handlers before re-adding them.
Distinguish Python Logs from TensorFlow C++ Logs
Not every noisy TensorFlow output is a Python logging duplication issue. Some startup messages come from TensorFlow's lower-level runtime. Those are usually controlled separately with the TF_CPP_MIN_LOG_LEVEL environment variable.
This reduces lower-level TensorFlow runtime verbosity, but it does not solve duplicate Python logging handlers. The two problems are related only in appearance, not in cause.
That distinction matters because many fixes appear to "reduce the noise" without actually correcting the logging topology. If your own application logs still duplicate, the handler tree still needs to be cleaned up.
Common Pitfalls
The most common mistake is calling basicConfig and also attaching custom handlers manually without checking what handlers already exist.
Another issue is fixing the TensorFlow logger but leaving propagation enabled to a root logger that already prints to the same destination. That still produces doubled output.
People also sometimes set TF_CPP_MIN_LOG_LEVEL and expect it to solve all logging duplication. It only affects certain runtime logs, not Python handler configuration.
Summary
- Doubled TensorFlow log lines usually come from Python logging configuration, not from TensorFlow emitting the same message twice deliberately.
- Check
tf.get_logger()and the root logger for duplicate handlers. - Remove redundant handlers or make the configuration idempotent.
- Use
logger.propagate = Falsewhen you want the TensorFlow logger to own its output path. - Use
TF_CPP_MIN_LOG_LEVELonly for lower-level runtime verbosity, not as a fix for Python logging duplication.
Related reading
- Tensorflow Check failed status CUDNN_STATUS_SUCCESS 7 vs. 0Failed to set cuDNN stream
- tensorflow check if a scalar boolean tensor is True
- tensorflow cifar10_eval.py errorRuntimeError Attempted to use a closed Session.RuntimeError Attempted to use a closed Session
- Tensorflow cnn error logits and labels must be same size
- tensorflow code optimization strategy
- Tensorflow compatibility with Keras
- Tensorflow Cross Device Communication
- Tensorflow Different ways to Export and Run graph in C

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.