Tensorflow抑制日志消息错误 [英] Tensorflow suppresses logging messages bug

查看:540
本文介绍了Tensorflow抑制日志消息错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Tensorflow使运行我的代码时隐藏并不会显示日志消息.

Tensorflow makes logging messages hide and not appear when I run the code.

我尝试了以下内容,但找不到使我的代码正常工作的方法.

I have tried the following stuff but couldn't find a way to make my code work.

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中,但示例日志中什么也没有出现. 当我导入tensorflow时,消息不会出现,而当我不出现时,消息就会出现.

I expected to have the debug messages into my file example.log but nothing appeared inside example log. When I import tensorflow, the messages don't appear and when I don't they do.

我需要同时使用tensorflow和日志记录,因为我使用的是现有代码.有什么方法可以使日志记录抑制Tensorflow吗?

I need to use both tensorflow and logging because I use an existing code. Is there a way so logging suppresses Tensorflow?

推荐答案

两个事实:

  1. logging.basicConfig 无效如果已经配置了根记录器:

  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.

  • tensorflow具有absl-py依赖性,在导入

  • 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)
    

    虽然不确定为什么将处理程序附加到根记录器而不是absl记录器,但这可能是错误或某些其他问题的解决方法.

    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之前配置日志记录:

    So the problem is that the import tensorflow call will call import absl.logging which causes an early logger configuration. The subsequent call (yours) to logging.basicConfig will hence do nothing. To fix that, you need to configure logging before importing 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')
    

    经验法则:始终尽早调用日志记录配置.

    Rule of thumb: always call your logging configuration as early as possible.

    如果您只想将默认日志写入文件,abseil记录器也可以执行以下操作:

    If you just want to write the default logs to file, abseil logger can also do that:

    from absl import logging as absl_logging
    
    absl_logging.get_absl_handler().use_absl_log_file(
        program_name='mytool',
        log_dir='/var/logs/'
    )
    

    这篇关于Tensorflow抑制日志消息错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆