【问题标题】:TensorFlow logs not showing up in Console and FileTensorFlow 日志未显示在控制台和文件中
【发布时间】:2016-09-20 08:59:44
【问题描述】:

这可能只是我的一个愚蠢的错误,但我似乎找不到它。

我想在我的控制台和日志文件中查看来自 TensorFlow 的日志,它适用于我的所有代码,但 TensorFlow 部分除外。

我已经像这样配置日志记录:

from logging.config import dictConfig
...
# Setup Logging
LOGGING_CONFIG = dict(
    version=1,
    formatters={
        # For files
        'detailed': {'format':
              '%(asctime)s %(name)-12s %(levelname)-8s %(message)s'},
        # For the console
        'console': {'format':
              '[%(levelname)s] %(message)s'}
    },
    handlers={
        'console': {
            'class': 'logging.StreamHandler',
            'level': logging.DEBUG,
            'formatter': 'console',
        },
        'file': {
            'class': 'logging.handlers.RotatingFileHandler',
            'level': logging.DEBUG,
            'formatter': 'detailed',
            'filename': LOG_FILE,
            'mode': 'a',
            'maxBytes': 10485760,  # 10 MB
            'backupCount': 5
        }
    },
    root={
        'handlers': ['console', 'file'],
        'level': logging.DEBUG,
    },
)
dictConfig(LOGGING_CONFIG)

我研究了这个问题并了解到我必须在 TensorFlow 中启用登录功能,如下所示:

import tensorflow as tf
...
tf.logging.set_verbosity(tf.logging.INFO)

不幸的是,这似乎不起作用 - 日志没有显示。如果我使用logging.basicConfig() 而不是我自己的配置,则日志将按预期显示。在这种情况下,日志将打印到我的终端。

我的结论是,我的日志记录配置有些错误 - 请帮助我。

【问题讨论】:

    标签: python python-3.x logging tensorflow


    【解决方案1】:

    看完Good logging practice in Python我发现了我的错误。默认情况下,对dictConfig 的调用会禁用现有记录器 - 我必须在配置中添加另一个键来解决此问题 (disable_existing_loggers):

    # Setup Logging
    LOGGING_CONFIG = dict(
        version=1,
        formatters={
            # For files
            'detailed': {'format':
                  '%(asctime)s %(name)-12s %(levelname)-8s %(message)s'},
            # For the console
            'console': {'format':
                  '[%(levelname)s] %(message)s'}
        },
        handlers={
            'console': {
                'class': 'logging.StreamHandler',
                'level': logging.DEBUG,
                'formatter': 'console',
            },
            'file': {
                'class': 'logging.handlers.RotatingFileHandler',
                'level': logging.DEBUG,
                'formatter': 'detailed',
                'filename': LOG_FILE,
                'mode': 'a',
                'maxBytes': 10485760,  # 10 MB
                'backupCount': 5
            }
        },
        root={
            'handlers': ['console', 'file'],
            'level': logging.DEBUG,
        },
        disable_existing_loggers=False
    )
    

    现在可以了:)

    【讨论】:

      猜你喜欢
      • 2015-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 2016-07-19
      • 1970-01-01
      相关资源
      最近更新 更多