Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom logging handlers in TensorFlow 1.8

Tags:

tensorflow

For previous versions of TensorFlow, we've been overriding handlers of tf.logging._logger to get custom logging behavior used in the rest of our python codebase:

  • standardized log format
  • log at level INFO to stdout and DEBUG to a file
  • on GCP, log to Stackdriver with an easy-to-search string prepended

For example, on TF 1.7, the following snippet (some logging boilerplate omitted)

    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")

produces

[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

In TF 1.8, tf.logging._logger is no longer accessible. Based on this thread, I tried setting

   tf_logging = _logging.getLogger('tensorflow')

in the above snippet, but the output is

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

I don't see any changes to tf_logging.py between TF 1.7 and 1.8 . I assume the tf_export calls weren't being enforced before, but are being enforced now. Is there still some way to override handlers for tf.logging? Alternatively, is there a more canonical way to get the above functionality?

like image 564
johnmcs Avatar asked May 18 '18 15:05

johnmcs


1 Answers

You can still access the tensorflow logger by directly importing the logging module:

from tensorflow.python.platform import tf_logging
tf_logger = tf_logging._get_logger()
tf_logger.handlers = handlers
like image 142
Filippo Scopel Avatar answered Oct 05 '22 07:10

Filippo Scopel