【问题标题】:Custom logging handlers in TensorFlow 1.8TensorFlow 1.8 中的自定义日志处理程序
【发布时间】:2018-10-29 02:11:32
【问题描述】:

对于以前版本的 TensorFlow,我们一直在覆盖 tf.logging._logger 的处理程序,以获取在我们的 Python 代码库的其余部分中使用的自定义日志记录行为:

  • 标准化日志格式
  • 将 INFO 级别的日志记录到 stdout 和 DEBUG 到文件
  • 在 GCP 上,登录到 Stackdriver,并在前面添加易于搜索的字符串

例如,在 TF 1.7 上,以下 sn-p(省略了一些日志记录样板)

    import tensorflow as tf
    tf.logging.set_verbosity(tf.logging.INFO)
    tf_logging = tf.logging._logger
    tf_logging.handlers = [_std_out_handler()]
    tf_logging.propagate = False
    _logging.info("EXPECTED LOG FORMAT")
    tf.logging.info("TF LOG FORMAT")

产生

[INFO |mrtx] 2018-05-18 15:10:07,613                                            /merantix_core/common/util/logging.py:189  --- EXPECTED LOG FORMAT
[INFO |mrtx] 2018-05-18 15:10:07,614  /usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/tf_logging.py:116  --- TF LOG FORMAT

在 TF 1.8 中,tf.logging._logger 不再可访问。基于这个thread,我尝试设置

   tf_logging = _logging.getLogger('tensorflow')

在上面的sn-p中,但是输出是

[INFO |mrtx] 2018-05-18 15:20:34,043                                            /merantix_core/common/util/logging.py:190  --- EXPECTED LOG FORMAT
INFO:tensorflow:TF LOG FORMAT

我没有看到 TF 1.7 和 1.8 之间的 tf_logging.py 有任何变化。我假设 tf_export 调用以前没有被强制执行,但现在正在强制执行。还有一些方法可以覆盖tf.logging 的处理程序吗?或者,是否有更规范的方式来获得上述功能?

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    您仍然可以通过直接导入日志记录模块来访问 tensorflow 记录器:

    from tensorflow.python.platform import tf_logging
    tf_logger = tf_logging._get_logger()
    tf_logger.handlers = handlers
    

    【讨论】:

      【解决方案2】:

      以下代码使用 tensorflow 1.12.0 测试良好。

      import tensorflow as tf
      import logging
      
      logger = logging.getLogger('tensorflow')
      formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
      for h in logger.handlers:
          h.setFormatter(formatter)
      tf.logging.set_verbosity(tf.logging.INFO)
      tf.logging.info("testing")
      tf.logging.debug("debug mesg")
      tf.logging.warning("a warning mesg")
      

      当我们导入 tensorflow 时,总会有一个记录器名称“tensorflow”。我们需要调用它并改变它的格式。

      Logger.handlers 只包含一个处理程序。

      tf.logging._get_logger() 在 tf 1.12.0 中不起作用。

      我们可以修改上面的代码,将处理程序(steam、文件、电子邮件等)添加到 tensorflow logger。

      【讨论】:

      • 它也适用于 tf1.15
      猜你喜欢
      • 1970-01-01
      • 2016-10-03
      • 1970-01-01
      • 2019-03-11
      • 2012-03-02
      • 1970-01-01
      • 2022-06-21
      • 1970-01-01
      • 2021-09-27
      相关资源
      最近更新 更多