console.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Copyright 2006 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 Simple logger that logs to the window console if available.
  16. *
  17. * Has an autoInstall option which can be put into initialization code, which
  18. * will start logging if "Debug=true" is in document.location.href
  19. *
  20. */
  21. goog.provide('goog.debug.Console');
  22. goog.require('goog.debug.LogManager');
  23. goog.require('goog.debug.Logger');
  24. goog.require('goog.debug.TextFormatter');
  25. /**
  26. * Create and install a log handler that logs to window.console if available
  27. * @constructor
  28. */
  29. goog.debug.Console = function() {
  30. this.publishHandler_ = goog.bind(this.addLogRecord, this);
  31. /**
  32. * Formatter for formatted output.
  33. * @type {!goog.debug.TextFormatter}
  34. * @private
  35. */
  36. this.formatter_ = new goog.debug.TextFormatter();
  37. this.formatter_.showAbsoluteTime = false;
  38. this.formatter_.showExceptionText = false;
  39. // The console logging methods automatically append a newline.
  40. this.formatter_.appendNewline = false;
  41. this.isCapturing_ = false;
  42. this.logBuffer_ = '';
  43. /**
  44. * Loggers that we shouldn't output.
  45. * @type {!Object<boolean>}
  46. * @private
  47. */
  48. this.filteredLoggers_ = {};
  49. };
  50. /**
  51. * Returns the text formatter used by this console
  52. * @return {!goog.debug.TextFormatter} The text formatter.
  53. */
  54. goog.debug.Console.prototype.getFormatter = function() {
  55. return this.formatter_;
  56. };
  57. /**
  58. * Sets whether we are currently capturing logger output.
  59. * @param {boolean} capturing Whether to capture logger output.
  60. */
  61. goog.debug.Console.prototype.setCapturing = function(capturing) {
  62. if (capturing == this.isCapturing_) {
  63. return;
  64. }
  65. // attach or detach handler from the root logger
  66. var rootLogger = goog.debug.LogManager.getRoot();
  67. if (capturing) {
  68. rootLogger.addHandler(this.publishHandler_);
  69. } else {
  70. rootLogger.removeHandler(this.publishHandler_);
  71. this.logBuffer = '';
  72. }
  73. this.isCapturing_ = capturing;
  74. };
  75. /**
  76. * Adds a log record.
  77. * @param {goog.debug.LogRecord} logRecord The log entry.
  78. */
  79. goog.debug.Console.prototype.addLogRecord = function(logRecord) {
  80. // Check to see if the log record is filtered or not.
  81. if (this.filteredLoggers_[logRecord.getLoggerName()]) {
  82. return;
  83. }
  84. var record = this.formatter_.formatRecord(logRecord);
  85. var console = goog.debug.Console.console_;
  86. if (console) {
  87. switch (logRecord.getLevel()) {
  88. case goog.debug.Logger.Level.SHOUT:
  89. goog.debug.Console.logToConsole_(console, 'info', record);
  90. break;
  91. case goog.debug.Logger.Level.SEVERE:
  92. goog.debug.Console.logToConsole_(console, 'error', record);
  93. break;
  94. case goog.debug.Logger.Level.WARNING:
  95. goog.debug.Console.logToConsole_(console, 'warn', record);
  96. break;
  97. default:
  98. goog.debug.Console.logToConsole_(console, 'debug', record);
  99. break;
  100. }
  101. } else {
  102. this.logBuffer_ += record;
  103. }
  104. };
  105. /**
  106. * Adds a logger name to be filtered.
  107. * @param {string} loggerName the logger name to add.
  108. */
  109. goog.debug.Console.prototype.addFilter = function(loggerName) {
  110. this.filteredLoggers_[loggerName] = true;
  111. };
  112. /**
  113. * Removes a logger name to be filtered.
  114. * @param {string} loggerName the logger name to remove.
  115. */
  116. goog.debug.Console.prototype.removeFilter = function(loggerName) {
  117. delete this.filteredLoggers_[loggerName];
  118. };
  119. /**
  120. * Global console logger instance
  121. * @type {goog.debug.Console}
  122. */
  123. goog.debug.Console.instance = null;
  124. /**
  125. * The console to which to log. This is a property so it can be mocked out in
  126. * this unit test for goog.debug.Console. Using goog.global, as console might be
  127. * used in window-less contexts.
  128. * @type {!{log:!Function}}
  129. * @private
  130. */
  131. goog.debug.Console.console_ = goog.global['console'];
  132. /**
  133. * Sets the console to which to log.
  134. * @param {!Object} console The console to which to log.
  135. */
  136. goog.debug.Console.setConsole = function(console) {
  137. goog.debug.Console.console_ = /** @type {!{log:!Function}} */ (console);
  138. };
  139. /**
  140. * Install the console and start capturing if "Debug=true" is in the page URL
  141. */
  142. goog.debug.Console.autoInstall = function() {
  143. if (!goog.debug.Console.instance) {
  144. goog.debug.Console.instance = new goog.debug.Console();
  145. }
  146. if (goog.global.location &&
  147. goog.global.location.href.indexOf('Debug=true') != -1) {
  148. goog.debug.Console.instance.setCapturing(true);
  149. }
  150. };
  151. /**
  152. * Show an alert with all of the captured debug information.
  153. * Information is only captured if console is not available
  154. */
  155. goog.debug.Console.show = function() {
  156. alert(goog.debug.Console.instance.logBuffer_);
  157. };
  158. /**
  159. * Logs the record to the console using the given function. If the function is
  160. * not available on the console object, the log function is used instead.
  161. * @param {!{log:!Function}} console The console object.
  162. * @param {string} fnName The name of the function to use.
  163. * @param {string} record The record to log.
  164. * @private
  165. */
  166. goog.debug.Console.logToConsole_ = function(console, fnName, record) {
  167. if (console[fnName]) {
  168. console[fnName](record);
  169. } else {
  170. console.log(record);
  171. }
  172. };