Path: blob/trunk/third_party/closure/goog/log/log.js
2868 views
// Copyright 2013 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Basic strippable logging definitions.16* @see http://go/closurelogging17*18* @author [email protected] (John Lenz)19*/2021goog.provide('goog.log');22goog.provide('goog.log.Level');23goog.provide('goog.log.LogRecord');24goog.provide('goog.log.Logger');2526goog.require('goog.debug');27goog.require('goog.debug.LogManager');28goog.require('goog.debug.LogRecord');29goog.require('goog.debug.Logger');303132/** @define {boolean} Whether logging is enabled. */33goog.define('goog.log.ENABLED', goog.debug.LOGGING_ENABLED);343536/** @const {string} */37goog.log.ROOT_LOGGER_NAME = goog.debug.Logger.ROOT_LOGGER_NAME;38394041/**42* @constructor43* @final44*/45goog.log.Logger = goog.debug.Logger;46474849/**50* @constructor51* @final52*/53goog.log.Level = goog.debug.Logger.Level;54555657/**58* @constructor59* @final60*/61goog.log.LogRecord = goog.debug.LogRecord;626364/**65* Finds or creates a logger for a named subsystem. If a logger has already been66* created with the given name it is returned. Otherwise a new logger is67* created. If a new logger is created its log level will be configured based68* on the goog.debug.LogManager configuration and it will configured to also69* send logging output to its parent's handlers.70* @see goog.debug.LogManager71*72* @param {string} name A name for the logger. This should be a dot-separated73* name and should normally be based on the package name or class name of74* the subsystem, such as goog.net.BrowserChannel.75* @param {goog.log.Level=} opt_level If provided, override the76* default logging level with the provided level.77* @return {goog.log.Logger} The named logger or null if logging is disabled.78*/79goog.log.getLogger = function(name, opt_level) {80if (goog.log.ENABLED) {81var logger = goog.debug.LogManager.getLogger(name);82if (opt_level && logger) {83logger.setLevel(opt_level);84}85return logger;86} else {87return null;88}89};909192// TODO(johnlenz): try to tighten the types to these functions.93/**94* Adds a handler to the logger. This doesn't use the event system because95* we want to be able to add logging to the event system.96* @param {goog.log.Logger} logger97* @param {Function} handler Handler function to add.98*/99goog.log.addHandler = function(logger, handler) {100if (goog.log.ENABLED && logger) {101logger.addHandler(handler);102}103};104105106/**107* Removes a handler from the logger. This doesn't use the event system because108* we want to be able to add logging to the event system.109* @param {goog.log.Logger} logger110* @param {Function} handler Handler function to remove.111* @return {boolean} Whether the handler was removed.112*/113goog.log.removeHandler = function(logger, handler) {114if (goog.log.ENABLED && logger) {115return logger.removeHandler(handler);116} else {117return false;118}119};120121122/**123* Logs a message. If the logger is currently enabled for the124* given message level then the given message is forwarded to all the125* registered output Handler objects.126* @param {goog.log.Logger} logger127* @param {goog.log.Level} level One of the level identifiers.128* @param {goog.debug.Loggable} msg The message to log.129* @param {Error|Object=} opt_exception An exception associated with the130* message.131*/132goog.log.log = function(logger, level, msg, opt_exception) {133if (goog.log.ENABLED && logger) {134logger.log(level, msg, opt_exception);135}136};137138139/**140* Logs a message at the Level.SEVERE level.141* If the logger is currently enabled for the given message level then the142* given message is forwarded to all the registered output Handler objects.143* @param {goog.log.Logger} logger144* @param {goog.debug.Loggable} msg The message to log.145* @param {Error=} opt_exception An exception associated with the message.146*/147goog.log.error = function(logger, msg, opt_exception) {148if (goog.log.ENABLED && logger) {149logger.severe(msg, opt_exception);150}151};152153154/**155* Logs a message at the Level.WARNING level.156* If the logger is currently enabled for the given message level then the157* given message is forwarded to all the registered output Handler objects.158* @param {goog.log.Logger} logger159* @param {goog.debug.Loggable} msg The message to log.160* @param {Error=} opt_exception An exception associated with the message.161*/162goog.log.warning = function(logger, msg, opt_exception) {163if (goog.log.ENABLED && logger) {164logger.warning(msg, opt_exception);165}166};167168169/**170* Logs a message at the Level.INFO level.171* If the logger is currently enabled for the given message level then the172* given message is forwarded to all the registered output Handler objects.173* @param {goog.log.Logger} logger174* @param {goog.debug.Loggable} msg The message to log.175* @param {Error=} opt_exception An exception associated with the message.176*/177goog.log.info = function(logger, msg, opt_exception) {178if (goog.log.ENABLED && logger) {179logger.info(msg, opt_exception);180}181};182183184/**185* Logs a message at the Level.Fine level.186* If the logger is currently enabled for the given message level then the187* given message is forwarded to all the registered output Handler objects.188* @param {goog.log.Logger} logger189* @param {goog.debug.Loggable} msg The message to log.190* @param {Error=} opt_exception An exception associated with the message.191*/192goog.log.fine = function(logger, msg, opt_exception) {193if (goog.log.ENABLED && logger) {194logger.fine(msg, opt_exception);195}196};197198199