Tensorflow导致日志记录消息加倍 [英] Tensorflow causes logging messages to double
问题描述
所以我在玩昨天他们发布的Google的 Tensorflow 库,并遇到了一个令人讨厌的错误,该错误一直困扰着我
我所做的就是像往常一样设置python日志记录功能,结果是,如果我导入tensorflow库,控制台中的所有消息都会开始加倍.有趣的是,如果仅使用logging.warn/info/..()
函数,则不会发生 .
不使消息加倍的代码示例:
import tensorflow as tf
import logging
logging.warn('test')
将所有消息加倍的代码示例:
import tensorflow as tf
import logging
logger = logging.getLogger('TEST')
ch = logging.StreamHandler()
logger.addHandler(ch)
logger.warn('test')
现在,我是一个简单的人.我喜欢logging
的功能,所以我使用它.通过查看其他人的操作方式,可以了解到使用logger
对象进行设置并添加了StreamHandler
的过程,但是看起来它与该事物的使用方式相吻合.但是,我对日志记录库没有深入的了解,因为它总是可以工作的.
因此,任何有助于解释为什么消息加倍的帮助都将是最有用的.
我正在将Ubuntu 14.04.3 LTS与Python 2.7.6结合使用,但是该错误在我尝试过的所有Python 2.7版本中都会发生.
我得到以下输出:
test
WARNING:TEST:test
Tensorflow也使用日志记录框架,并且已经设置了自己的处理程序,因此默认情况下,当您记录日志时,它会传播到tensorflow内部的父日志记录处理程序.您可以通过以下方式更改此行为:
logger.propagate = False
另请参见在简单的python日志记录配置中重复输出 >
跟进:这是tensorflow使用日志记录包的方式的意外副作用.为了避免这种污染,我在HEAD处对其进行了更改,以使其内部记录器的名称为"tensorflow".应该在一天左右的时间内放在github头上.同时,logger.propagate解决方案将起作用,并且在修复该问题后也不会中断,因此您应该放心.再次感谢您发现这个问题!
跟进跟进:
从logger
:
import tensorflow as tf
logger = tf.get_logger()
So I was playing around with Google's Tensorflow library they published yesterday and encountered an annoying bug that keeps biting me.
What I did was setup the python logging functions as I usually do, and the result was that, if I import the tensorflow library, all messages in the console started doubling. Interestingly, this does not happen if you just use the logging.warn/info/..()
function.
An example of a code that does not double the messages:
import tensorflow as tf
import logging
logging.warn('test')
An example of a code that does double all messages:
import tensorflow as tf
import logging
logger = logging.getLogger('TEST')
ch = logging.StreamHandler()
logger.addHandler(ch)
logger.warn('test')
Now, I'm a simple man. I like the functionality of logging
, so I use it. The setup with the logger
object and the adding of a StreamHandler
is something I picked up looking at how other people did this, but it looks like it fits with how the thing was meant to be used. However, I do not have in-depth knowledge of the logging library, as it always just kind of worked.
So, any help explaining why the doubling of the messages occurs will be most helpful.
I am using Ubuntu 14.04.3 LTS with Python 2.7.6, but the error happens in all Python 2.7 versions I tried.
I get this output:
test
WARNING:TEST:test
Tensorflow is also using the logging framework and has set up its own handlers, so when you log, by default, it propagates up to the parent logging handlers inside tensorflow. You can change this behavior by setting:
logger.propagate = False
See also duplicate output in simple python logging configuration
Followup: This was an unintended side-effect of the way tensorflow was using the logging package. I've changed it at HEAD to scope its internal loggers under the name "tensorflow" to avoid this pollution. Should be in the github head within a day or so. In the meantime, the logger.propagate solution will work and won't break once that fix is in, so you should be safe to go. Thanks again for spotting this!
Followup-Followup:
Starting with TensorFlow 1.14 exposes the logger
directly:
import tensorflow as tf
logger = tf.get_logger()
这篇关于Tensorflow导致日志记录消息加倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!