log.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright 2013 The Closure Library 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. * @fileoverview Basic strippable logging definitions.
  16. * @see http://go/closurelogging
  17. *
  18. * @author johnlenz@google.com (John Lenz)
  19. */
  20. goog.provide('goog.log');
  21. goog.provide('goog.log.Level');
  22. goog.provide('goog.log.LogRecord');
  23. goog.provide('goog.log.Logger');
  24. goog.require('goog.debug');
  25. goog.require('goog.debug.LogManager');
  26. goog.require('goog.debug.LogRecord');
  27. goog.require('goog.debug.Logger');
  28. /** @define {boolean} Whether logging is enabled. */
  29. goog.define('goog.log.ENABLED', goog.debug.LOGGING_ENABLED);
  30. /** @const {string} */
  31. goog.log.ROOT_LOGGER_NAME = goog.debug.Logger.ROOT_LOGGER_NAME;
  32. /**
  33. * @constructor
  34. * @final
  35. */
  36. goog.log.Logger = goog.debug.Logger;
  37. /**
  38. * @constructor
  39. * @final
  40. */
  41. goog.log.Level = goog.debug.Logger.Level;
  42. /**
  43. * @constructor
  44. * @final
  45. */
  46. goog.log.LogRecord = goog.debug.LogRecord;
  47. /**
  48. * Finds or creates a logger for a named subsystem. If a logger has already been
  49. * created with the given name it is returned. Otherwise a new logger is
  50. * created. If a new logger is created its log level will be configured based
  51. * on the goog.debug.LogManager configuration and it will configured to also
  52. * send logging output to its parent's handlers.
  53. * @see goog.debug.LogManager
  54. *
  55. * @param {string} name A name for the logger. This should be a dot-separated
  56. * name and should normally be based on the package name or class name of
  57. * the subsystem, such as goog.net.BrowserChannel.
  58. * @param {goog.log.Level=} opt_level If provided, override the
  59. * default logging level with the provided level.
  60. * @return {goog.log.Logger} The named logger or null if logging is disabled.
  61. */
  62. goog.log.getLogger = function(name, opt_level) {
  63. if (goog.log.ENABLED) {
  64. var logger = goog.debug.LogManager.getLogger(name);
  65. if (opt_level && logger) {
  66. logger.setLevel(opt_level);
  67. }
  68. return logger;
  69. } else {
  70. return null;
  71. }
  72. };
  73. // TODO(johnlenz): try to tighten the types to these functions.
  74. /**
  75. * Adds a handler to the logger. This doesn't use the event system because
  76. * we want to be able to add logging to the event system.
  77. * @param {goog.log.Logger} logger
  78. * @param {Function} handler Handler function to add.
  79. */
  80. goog.log.addHandler = function(logger, handler) {
  81. if (goog.log.ENABLED && logger) {
  82. logger.addHandler(handler);
  83. }
  84. };
  85. /**
  86. * Removes a handler from the logger. This doesn't use the event system because
  87. * we want to be able to add logging to the event system.
  88. * @param {goog.log.Logger} logger
  89. * @param {Function} handler Handler function to remove.
  90. * @return {boolean} Whether the handler was removed.
  91. */
  92. goog.log.removeHandler = function(logger, handler) {
  93. if (goog.log.ENABLED && logger) {
  94. return logger.removeHandler(handler);
  95. } else {
  96. return false;
  97. }
  98. };
  99. /**
  100. * Logs a message. If the logger is currently enabled for the
  101. * given message level then the given message is forwarded to all the
  102. * registered output Handler objects.
  103. * @param {goog.log.Logger} logger
  104. * @param {goog.log.Level} level One of the level identifiers.
  105. * @param {goog.debug.Loggable} msg The message to log.
  106. * @param {Error|Object=} opt_exception An exception associated with the
  107. * message.
  108. */
  109. goog.log.log = function(logger, level, msg, opt_exception) {
  110. if (goog.log.ENABLED && logger) {
  111. logger.log(level, msg, opt_exception);
  112. }
  113. };
  114. /**
  115. * Logs a message at the Level.SEVERE level.
  116. * If the logger is currently enabled for the given message level then the
  117. * given message is forwarded to all the registered output Handler objects.
  118. * @param {goog.log.Logger} logger
  119. * @param {goog.debug.Loggable} msg The message to log.
  120. * @param {Error=} opt_exception An exception associated with the message.
  121. */
  122. goog.log.error = function(logger, msg, opt_exception) {
  123. if (goog.log.ENABLED && logger) {
  124. logger.severe(msg, opt_exception);
  125. }
  126. };
  127. /**
  128. * Logs a message at the Level.WARNING level.
  129. * If the logger is currently enabled for the given message level then the
  130. * given message is forwarded to all the registered output Handler objects.
  131. * @param {goog.log.Logger} logger
  132. * @param {goog.debug.Loggable} msg The message to log.
  133. * @param {Error=} opt_exception An exception associated with the message.
  134. */
  135. goog.log.warning = function(logger, msg, opt_exception) {
  136. if (goog.log.ENABLED && logger) {
  137. logger.warning(msg, opt_exception);
  138. }
  139. };
  140. /**
  141. * Logs a message at the Level.INFO level.
  142. * If the logger is currently enabled for the given message level then the
  143. * given message is forwarded to all the registered output Handler objects.
  144. * @param {goog.log.Logger} logger
  145. * @param {goog.debug.Loggable} msg The message to log.
  146. * @param {Error=} opt_exception An exception associated with the message.
  147. */
  148. goog.log.info = function(logger, msg, opt_exception) {
  149. if (goog.log.ENABLED && logger) {
  150. logger.info(msg, opt_exception);
  151. }
  152. };
  153. /**
  154. * Logs a message at the Level.Fine level.
  155. * If the logger is currently enabled for the given message level then the
  156. * given message is forwarded to all the registered output Handler objects.
  157. * @param {goog.log.Logger} logger
  158. * @param {goog.debug.Loggable} msg The message to log.
  159. * @param {Error=} opt_exception An exception associated with the message.
  160. */
  161. goog.log.fine = function(logger, msg, opt_exception) {
  162. if (goog.log.ENABLED && logger) {
  163. logger.fine(msg, opt_exception);
  164. }
  165. };