123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- // Copyright 2013 The Closure Library Authors. All Rights Reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS-IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- /**
- * @fileoverview Basic strippable logging definitions.
- * @see http://go/closurelogging
- *
- * @author johnlenz@google.com (John Lenz)
- */
- goog.provide('goog.log');
- goog.provide('goog.log.Level');
- goog.provide('goog.log.LogRecord');
- goog.provide('goog.log.Logger');
- goog.require('goog.debug');
- goog.require('goog.debug.LogManager');
- goog.require('goog.debug.LogRecord');
- goog.require('goog.debug.Logger');
- /** @define {boolean} Whether logging is enabled. */
- goog.define('goog.log.ENABLED', goog.debug.LOGGING_ENABLED);
- /** @const {string} */
- goog.log.ROOT_LOGGER_NAME = goog.debug.Logger.ROOT_LOGGER_NAME;
- /**
- * @constructor
- * @final
- */
- goog.log.Logger = goog.debug.Logger;
- /**
- * @constructor
- * @final
- */
- goog.log.Level = goog.debug.Logger.Level;
- /**
- * @constructor
- * @final
- */
- goog.log.LogRecord = goog.debug.LogRecord;
- /**
- * Finds or creates a logger for a named subsystem. If a logger has already been
- * created with the given name it is returned. Otherwise a new logger is
- * created. If a new logger is created its log level will be configured based
- * on the goog.debug.LogManager configuration and it will configured to also
- * send logging output to its parent's handlers.
- * @see goog.debug.LogManager
- *
- * @param {string} name A name for the logger. This should be a dot-separated
- * name and should normally be based on the package name or class name of
- * the subsystem, such as goog.net.BrowserChannel.
- * @param {goog.log.Level=} opt_level If provided, override the
- * default logging level with the provided level.
- * @return {goog.log.Logger} The named logger or null if logging is disabled.
- */
- goog.log.getLogger = function(name, opt_level) {
- if (goog.log.ENABLED) {
- var logger = goog.debug.LogManager.getLogger(name);
- if (opt_level && logger) {
- logger.setLevel(opt_level);
- }
- return logger;
- } else {
- return null;
- }
- };
- // TODO(johnlenz): try to tighten the types to these functions.
- /**
- * Adds a handler to the logger. This doesn't use the event system because
- * we want to be able to add logging to the event system.
- * @param {goog.log.Logger} logger
- * @param {Function} handler Handler function to add.
- */
- goog.log.addHandler = function(logger, handler) {
- if (goog.log.ENABLED && logger) {
- logger.addHandler(handler);
- }
- };
- /**
- * Removes a handler from the logger. This doesn't use the event system because
- * we want to be able to add logging to the event system.
- * @param {goog.log.Logger} logger
- * @param {Function} handler Handler function to remove.
- * @return {boolean} Whether the handler was removed.
- */
- goog.log.removeHandler = function(logger, handler) {
- if (goog.log.ENABLED && logger) {
- return logger.removeHandler(handler);
- } else {
- return false;
- }
- };
- /**
- * Logs a message. If the logger is currently enabled for the
- * given message level then the given message is forwarded to all the
- * registered output Handler objects.
- * @param {goog.log.Logger} logger
- * @param {goog.log.Level} level One of the level identifiers.
- * @param {goog.debug.Loggable} msg The message to log.
- * @param {Error|Object=} opt_exception An exception associated with the
- * message.
- */
- goog.log.log = function(logger, level, msg, opt_exception) {
- if (goog.log.ENABLED && logger) {
- logger.log(level, msg, opt_exception);
- }
- };
- /**
- * Logs a message at the Level.SEVERE level.
- * If the logger is currently enabled for the given message level then the
- * given message is forwarded to all the registered output Handler objects.
- * @param {goog.log.Logger} logger
- * @param {goog.debug.Loggable} msg The message to log.
- * @param {Error=} opt_exception An exception associated with the message.
- */
- goog.log.error = function(logger, msg, opt_exception) {
- if (goog.log.ENABLED && logger) {
- logger.severe(msg, opt_exception);
- }
- };
- /**
- * Logs a message at the Level.WARNING level.
- * If the logger is currently enabled for the given message level then the
- * given message is forwarded to all the registered output Handler objects.
- * @param {goog.log.Logger} logger
- * @param {goog.debug.Loggable} msg The message to log.
- * @param {Error=} opt_exception An exception associated with the message.
- */
- goog.log.warning = function(logger, msg, opt_exception) {
- if (goog.log.ENABLED && logger) {
- logger.warning(msg, opt_exception);
- }
- };
- /**
- * Logs a message at the Level.INFO level.
- * If the logger is currently enabled for the given message level then the
- * given message is forwarded to all the registered output Handler objects.
- * @param {goog.log.Logger} logger
- * @param {goog.debug.Loggable} msg The message to log.
- * @param {Error=} opt_exception An exception associated with the message.
- */
- goog.log.info = function(logger, msg, opt_exception) {
- if (goog.log.ENABLED && logger) {
- logger.info(msg, opt_exception);
- }
- };
- /**
- * Logs a message at the Level.Fine level.
- * If the logger is currently enabled for the given message level then the
- * given message is forwarded to all the registered output Handler objects.
- * @param {goog.log.Logger} logger
- * @param {goog.debug.Loggable} msg The message to log.
- * @param {Error=} opt_exception An exception associated with the message.
- */
- goog.log.fine = function(logger, msg, opt_exception) {
- if (goog.log.ENABLED && logger) {
- logger.fine(msg, opt_exception);
- }
- };
|