Tensorflow抑制日志消息错误

9

Tensorflow 使日志消息隐藏起来,代码运行时无法显示。

我尝试过以下方法,但无法找到让我的代码工作的方法。

import logging
logger = tf.get_logger()
logger.setLevel(logging.ERROR)


import os
import tensorflow as tf

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

我的代码如下:

import logging
import tensorflow as tf

logging.basicConfig(filename='example.log', level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')

我希望将调试信息输出到我的文件example.log中,但是在example log中没有看到任何内容。 当我导入tensorflow时,这些消息不会出现,当我不导入时,它们会出现。
因为我使用现有代码,所以需要同时使用tensorflow和logging。是否有一种方法可以使logging抑制Tensorflow?
2个回答

5

两个事实:

  1. logging.basicConfig will do nothing if the root logger is already configured:

    This function does nothing if the root logger already has handlers configured for it.

  2. tensorflow has the absl-py dependency that will try to initialize logging when imported by appending a NullHandler to the root handler:

    # The absl handler will always be attached to root, not the absl logger.
    if not logging.root.handlers:
      # Attach the absl handler at import time when there are no other handlers.
      # Otherwise it means users have explicitly configured logging, and the absl
      # handler will only be attached later in app.run(). For App Engine apps,
      # the absl handler is not used.
      logging.root.addHandler(_absl_handler)
    

    Not sure why the handler is attached to the root logger instead of the absl logger, though - might be a bug or a workaround for some other issue.

因此问题在于import tensorflow调用了import absl.logging,因此会导致早期的日志记录配置。后续调用(您的调用)logging.basicConfig将因此无效。为解决这个问题,您需要在导入tensorflow之前配置日志记录:
import logging
logging.basicConfig(filename='example.log', level=logging.DEBUG)
import tensorflow as tf

logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')

经验法则:尽可能早地调用日志配置。

以默认格式将日志写入文件

如果您只想将默认日志写入文件,abseil记录器也可以做到:

from absl import logging as absl_logging

absl_logging.get_absl_handler().use_absl_log_file(
    program_name='mytool',
    log_dir='/var/logs/'
)

1
除了 @hoefling 提供的方法,你还可以在配置日志记录之前清除根记录器的处理程序 handlers
logging.getLogger().handlers = []
# ...
logging.basicConfig(level=level, handlers=handlers)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接