TensorFlow
logging issues
debugging
programming
machine learning

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.

Practice ML system design

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.

python
1import logging
2import tensorflow as tf
3
4logger = tf.get_logger()
5logger.setLevel(logging.INFO)
6
7for handler in list(logger.handlers):
8    logger.removeHandler(handler)
9
10stream_handler = logging.StreamHandler()
11stream_handler.setLevel(logging.INFO)
12logger.addHandler(stream_handler)
13logger.propagate = False
14
15logger.info("TensorFlow logging is configured once")

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:

python
1import logging
2
3root = logging.getLogger()
4print(len(root.handlers))

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.

python
1import os
2os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
3
4import tensorflow as tf

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 = False when you want the TensorFlow logger to own its output path.
  • Use TF_CPP_MIN_LOG_LEVEL only for lower-level runtime verbosity, not as a fix for Python logging duplication.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice ML system design

All Rights Reserved.