1# Copyright 2015 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15"""tensorboard_logging provides logging that is also written to the events file. 16 17Any messages logged via this module will be logged both via the platform logging 18mechanism and to the SummaryWriter set via `set_summary_writer`. This is useful 19for logging messages that you might want to be visible from inside TensorBoard 20or that should be permanently associated with the training session. 21 22You can use this just like the logging module: 23 24``` 25tensorboard_logging.set_summary_writer(summary_writer) 26tensorboard_logging.info("my %s", "message") 27tensorboard_logging.log(tensorboard_logging.WARN, "something") 28``` 29""" 30 31from __future__ import absolute_import 32from __future__ import division 33from __future__ import print_function 34 35import time 36 37from tensorflow.core.util import event_pb2 38from tensorflow.python.platform import tf_logging as logging 39 40DEBUG = 'DEBUG' 41INFO = 'INFO' 42WARN = 'WARN' 43ERROR = 'ERROR' 44FATAL = 'FATAL' 45 46# Messages with levels below this verbosity will not be logged. 47_verbosity = WARN 48 49# A value meaning 'not set yet' so we can use None to mean 'user actively told 50# us they don't want a SummaryWriter'. 51_sentinel_summary_writer = object() 52 53# The SummaryWriter instance to use when logging, or None to not log, or 54# _sentinel_summary_writer to indicate that the user hasn't called 55# set_summary_writer yet. 56_summary_writer = _sentinel_summary_writer 57 58# Map from the tensorboard_logging logging enum values to the proto's enum 59# values. 60_LEVEL_PROTO_MAP = { 61 DEBUG: event_pb2.LogMessage.DEBUGGING, 62 INFO: event_pb2.LogMessage.INFO, 63 WARN: event_pb2.LogMessage.WARN, 64 ERROR: event_pb2.LogMessage.ERROR, 65 FATAL: event_pb2.LogMessage.FATAL, 66} 67 68# Map from the tensorboard_logging module levels to the logging module levels. 69_PLATFORM_LOGGING_LEVEL_MAP = { 70 DEBUG: logging.DEBUG, 71 INFO: logging.INFO, 72 WARN: logging.WARN, 73 ERROR: logging.ERROR, 74 FATAL: logging.FATAL 75} 76 77 78def get_verbosity(): 79 return _verbosity 80 81 82def set_verbosity(verbosity): 83 _check_verbosity(verbosity) 84 global _verbosity 85 _verbosity = verbosity 86 87 88def _check_verbosity(verbosity): 89 if verbosity not in _LEVEL_PROTO_MAP: 90 raise ValueError('Level %s is not a valid tensorboard_logging level' % 91 verbosity) 92 93 94def set_summary_writer(summary_writer): 95 """Sets the summary writer that events will be logged to. 96 97 Calling any logging methods inside this module without calling this method 98 will fail. If you don't want to log, call `set_summary_writer(None)`. 99 100 Args: 101 summary_writer: Either a SummaryWriter or None. None will cause messages not 102 to be logged to any SummaryWriter, but they will still be passed to the 103 platform logging module. 104 """ 105 global _summary_writer 106 _summary_writer = summary_writer 107 108 109def _clear_summary_writer(): 110 """Makes all subsequent log invocations error. 111 112 This is only used for testing. If you want to disable TensorBoard logging, 113 call `set_summary_writer(None)` instead. 114 """ 115 global _summary_writer 116 _summary_writer = _sentinel_summary_writer 117 118 119def log(level, message, *args): 120 """Conditionally logs `message % args` at the level `level`. 121 122 Note that tensorboard_logging verbosity and logging verbosity are separate; 123 the message will always be passed through to the logging module regardless of 124 whether it passes the tensorboard_logging verbosity check. 125 126 Args: 127 level: The verbosity level to use. Must be one of 128 tensorboard_logging.{DEBUG, INFO, WARN, ERROR, FATAL}. 129 message: The message template to use. 130 *args: Arguments to interpolate to the message template, if any. 131 132 Raises: 133 ValueError: If `level` is not a valid logging level. 134 RuntimeError: If the `SummaryWriter` to use has not been set. 135 """ 136 if _summary_writer is _sentinel_summary_writer: 137 raise RuntimeError('Must call set_summary_writer before doing any ' 138 'logging from tensorboard_logging') 139 _check_verbosity(level) 140 proto_level = _LEVEL_PROTO_MAP[level] 141 if proto_level >= _LEVEL_PROTO_MAP[_verbosity]: 142 log_message = event_pb2.LogMessage(level=proto_level, 143 message=message % args) 144 event = event_pb2.Event(wall_time=time.time(), log_message=log_message) 145 146 if _summary_writer: 147 _summary_writer.add_event(event) 148 149 logging.log(_PLATFORM_LOGGING_LEVEL_MAP[level], message, *args) 150 151 152def debug(message, *args): 153 log(DEBUG, message, *args) 154 155 156def info(message, *args): 157 log(INFO, message, *args) 158 159 160def warn(message, *args): 161 log(WARN, message, *args) 162 163 164def error(message, *args): 165 log(ERROR, message, *args) 166 167 168def fatal(message, *args): 169 log(FATAL, message, *args) 170