5

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?

johnmcs
  • 429
  • 4
  • 10

2 Answers2

6

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
2

The following code is tested well with 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")

When we import tensorflow, there is always a logger name 'tensorflow'. We need to call it and change its format.

Logger.handlers contains only one handler.

tf.logging._get_logger() does not work in tf 1.12.0.

We can modify the code above to add handlers (steam, file, email, etc) to tensorflow logger.

user44875
  • 356
  • 1
  • 7