SoFunction
Updated on 2024-12-10

A short summary of the several ways TensorFlow sets logging levels

There are 4 levels of log in TensorFlow, INFO, WARN, ERROR, and FATAL. There are several ways to set them as follows.

1. Control the log level by setting environment variables

It can be set by the environment variable TF_CPP_MIN_LOG_LEVEL. The meanings of different values of TF_CPP_MIN_LOG_LEVEL are as follows:

Level Level for Humans Level Description
0 DEBUG all messages are logged (Default)
1 INFO INFO messages are not printed
2 WARNING INFO and WARNING messages are not printed
3 ERROR INFO, WARNING, and ERROR messages are not printed

Set the value of TF_CPP_MIN_LOG_LEVEL to block the logs of this level and lower levels, e.g., set to 1 to block the INFO and DEBUG logs of its own level and lower levels.

To set environment variables, there are two ways, one permanent and one temporary.

Permanent settings

Modify environment variables: add export TF_CPP_MIN_LOG_LEVEL=1 to the Linux ~/.bashrc, ~/.zshrc or /etc/profile configuration file, where adding to the /etc/profile file is valid for all users. It can be set to 1 or 2 in the configuration file, and it is not recommended to set it to a higher level of 3.

Interim setup

Enter export TF_CPP_MIN_LOG_LEVEL="1" in the terminal.

The python code implements the method

import os
['TF_CPP_MIN_LOG_LEVEL'] = "1"

ps: is a mapping, a series of key, value pairs stored in the mapping, the system environment information is stored in the mapping. If it is print(['HOME']) so that the output information, the call is getenv("HOME") function, if the environment variable is changed, will call putenv() function to modify.

An incorrect way of setting up python code:

("export TF_CPP_MIN_LOG_LEVEL=1") - Error Usage

Reason why it doesn't work - you can't change shell environment variables through a child process of the shell - for details see* alexThe answer.

2. Setup via Module - Recommended

The python code can be set by adding the following section of code.

import tensorflow as tf
.set_verbosity()

can be replaced by any of {DEBUG, INFO, WARN, ERROR, FATAL}.

It is different here, setting ERROR will output ERROR, FATAL level logs.

Code Testing:

def main(self): 
  .set_verbosity()
  ("debug")
  ("info")
  ("warning")
  ("error")
  ("fatal")
  
if __name__ == '__main__':
  ()

Run the above code to get ERROR, FATAL level logs.

The above summary of several ways to set logging levels in TensorFlow is all I have to share with you, I hope it can give you a reference, and I hope you will support me more.