这可能只是我的愚蠢错误,但我似乎找不到它。
我想在控制台和日志文件中查看来自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的良好日志记录实践之后,我发现了我的错误。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
)
现在可以了:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句