TensorFlow
debugging
logging
information
disable

Disable Tensorflow debugging information

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

TensorFlow can print a lot of startup and runtime logging, especially from its C++ backend. The usual way to reduce that noise is to set TF_CPP_MIN_LOG_LEVEL before importing TensorFlow. After import, you can also adjust Python-side logging, but the order matters because many messages are emitted during import and initialization.

Use TF_CPP_MIN_LOG_LEVEL Before Import

The main environment variable is TF_CPP_MIN_LOG_LEVEL:

  • '0 shows all logs'
  • '1 hides INFO'
  • '2 hides INFO and WARNING'
  • '3 hides INFO, WARNING, and ERROR from the C++ backend'

Set it before importing TensorFlow:

python
1import os
2
3os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
4
5import tensorflow as tf
6
7print(tf.__version__)

If you set the variable after import tensorflow as tf, the early startup messages may already have been printed.

Shell-Level Configuration

If you want the setting to apply to a whole shell session, export it before running Python:

bash
export TF_CPP_MIN_LOG_LEVEL=2
python my_script.py

On Windows Command Prompt:

bat
set TF_CPP_MIN_LOG_LEVEL=2
python my_script.py

This is often cleaner for experiments because it avoids repeating the environment setup inside every script.

Adjust TensorFlow Python Logging Too

TensorFlow also exposes a Python logger. For some messages, especially those routed through Python logging, you can reduce verbosity there as well.

python
1import os
2os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
3
4import tensorflow as tf
5
6tf.get_logger().setLevel("ERROR")

This does not replace the environment variable. It complements it.

Know What You Are Suppressing

Hiding logs can make notebooks and training output easier to read, but it can also hide useful diagnostics. In development, 1 or 2 is often a better compromise than 3 because warnings can still matter.

For example, device placement, backend initialization, missing acceleration libraries, and deprecation notices may show up as messages you actually want to see while debugging environment issues.

Use Placement Logging Separately

Some TensorFlow output is not ordinary startup logging. For example, device placement traces are controlled separately:

python
import tensorflow as tf

tf.debugging.set_log_device_placement(False)

If you accidentally enabled device placement logs, changing TF_CPP_MIN_LOG_LEVEL alone may not be the whole solution.

In notebooks, this ordering requirement is especially important because TensorFlow may already have been imported in an earlier cell. If that happened, changing the environment variable later may appear to do nothing until the kernel is restarted.

The same rule applies in service startup code. If the process manager or container should control TensorFlow verbosity globally, setting the environment variable at process launch is usually more reliable than trying to patch logging after import inside application code.

It is also common to put the environment variable into a launch script or notebook kernel configuration so every run starts with the same verbosity instead of relying on each file to remember the setting.

Common Pitfalls

The biggest mistake is setting TF_CPP_MIN_LOG_LEVEL after importing TensorFlow. That is too late for many startup messages.

Another issue is assuming all TensorFlow-related text goes through the same logging path. Some messages come from the C++ backend, some from Python logging, and some from optional debugging features such as placement logging.

People also sometimes set the log level to 3 and then wonder why useful warnings disappeared. That may be desirable in a very noisy production environment, but it can make development harder.

Finally, do not confuse log suppression with fixing the underlying problem. If TensorFlow warns about a missing library or incompatible environment, hiding the message does not solve the issue.

Summary

  • Set TF_CPP_MIN_LOG_LEVEL before importing TensorFlow to suppress backend logs.
  • Use tf.get_logger().setLevel(...) to reduce Python-side logging too.
  • Configure the environment in the shell if you want the setting to apply broadly.
  • Treat device placement logs as a separate switch when needed.
  • Reduce noise carefully so you do not hide messages that are actually useful for debugging.

Course illustration
Course illustration

All Rights Reserved.