TensorFlow日志没有显示在控制台和文件
问题描述:
这可能只是我的一个愚蠢的错误,但我似乎无法找到它。TensorFlow日志没有显示在控制台和文件
我想在我的控制台和日志文件中看到来自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()
而不是我自己的配置,则会按预期显示日志。在这种情况下,日志将打印到我的终端。
我的结论是,我的日志配置有点错误 - 请帮助我。
答
看完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
)
现在工程:)