log_test.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 Unit tests for goog.log.
  16. */
  17. /** @suppress {extraProvide} */
  18. goog.provide('goog.logTest');
  19. goog.require('goog.debug.LogManager');
  20. goog.require('goog.log');
  21. goog.require('goog.log.Level');
  22. goog.require('goog.testing.jsunit');
  23. goog.setTestOnly('goog.logTest');
  24. /**
  25. * A simple log handler that remembers the last record published.
  26. * @constructor
  27. * @private
  28. */
  29. function TestHandler_() {
  30. this.logRecord = null;
  31. }
  32. TestHandler_.prototype.onPublish = function(logRecord) {
  33. this.logRecord = logRecord;
  34. };
  35. TestHandler_.prototype.reset = function() {
  36. this.logRecord = null;
  37. };
  38. function testParents() {
  39. var logger2sibling1 = goog.log.getLogger('goog.test');
  40. var logger2sibling2 = goog.log.getLogger('goog.bar');
  41. var logger3sibling1 = goog.log.getLogger('goog.bar.foo');
  42. var logger3siblint2 = goog.log.getLogger('goog.bar.baaz');
  43. var rootLogger = goog.debug.LogManager.getRoot();
  44. var googLogger = goog.log.getLogger('goog');
  45. assertEquals(rootLogger, googLogger.getParent());
  46. assertEquals(googLogger, logger2sibling1.getParent());
  47. assertEquals(googLogger, logger2sibling2.getParent());
  48. assertEquals(logger2sibling2, logger3sibling1.getParent());
  49. assertEquals(logger2sibling2, logger3siblint2.getParent());
  50. }
  51. function testLogging1() {
  52. var root = goog.debug.LogManager.getRoot();
  53. var handler = new TestHandler_();
  54. var f = goog.bind(handler.onPublish, handler);
  55. goog.log.addHandler(root, f);
  56. var logger = goog.log.getLogger('goog.bar.baaz');
  57. goog.log.log(logger, goog.log.Level.WARNING, 'foo');
  58. assertNotNull(handler.logRecord);
  59. assertEquals(goog.log.Level.WARNING, handler.logRecord.getLevel());
  60. assertEquals('foo', handler.logRecord.getMessage());
  61. handler.logRecord = null;
  62. goog.log.removeHandler(root, f);
  63. goog.log.log(logger, goog.log.Level.WARNING, 'foo');
  64. assertNull(handler.logRecord);
  65. }
  66. function testLogging2() {
  67. var root = goog.debug.LogManager.getRoot();
  68. var handler = new TestHandler_();
  69. var f = goog.bind(handler.onPublish, handler);
  70. goog.log.addHandler(root, f);
  71. var logger = goog.log.getLogger('goog.bar.baaz');
  72. goog.log.warning(logger, 'foo');
  73. assertNotNull(handler.logRecord);
  74. assertEquals(goog.log.Level.WARNING, handler.logRecord.getLevel());
  75. assertEquals('foo', handler.logRecord.getMessage());
  76. handler.logRecord = null;
  77. goog.log.removeHandler(root, f);
  78. goog.log.log(logger, goog.log.Level.WARNING, 'foo');
  79. assertNull(handler.logRecord);
  80. }
  81. function testFiltering() {
  82. var root = goog.debug.LogManager.getRoot();
  83. var handler = new TestHandler_();
  84. var f = goog.bind(handler.onPublish, handler);
  85. root.addHandler(f);
  86. var logger1 = goog.log.getLogger('goog.bar.foo', goog.log.Level.WARNING);
  87. var logger2 = goog.log.getLogger('goog.bar.baaz', goog.log.Level.INFO);
  88. goog.log.warning(logger2, 'foo');
  89. assertNotNull(handler.logRecord);
  90. assertEquals(goog.log.Level.WARNING, handler.logRecord.getLevel());
  91. assertEquals('foo', handler.logRecord.getMessage());
  92. handler.reset();
  93. goog.log.info(logger1, 'bar');
  94. assertNull(handler.logRecord);
  95. goog.log.warning(logger1, 'baaz');
  96. assertNotNull(handler.logRecord);
  97. handler.reset();
  98. goog.log.error(logger1, 'baaz');
  99. assertNotNull(handler.logRecord);
  100. }
  101. function testException() {
  102. var root = goog.debug.LogManager.getRoot();
  103. var handler = new TestHandler_();
  104. var f = goog.bind(handler.onPublish, handler);
  105. root.addHandler(f);
  106. var logger = goog.log.getLogger('goog.debug.logger_test');
  107. var ex = Error('boo!');
  108. goog.log.error(logger, 'hello', ex);
  109. assertNotNull(handler.logRecord);
  110. assertEquals(goog.log.Level.SEVERE, handler.logRecord.getLevel());
  111. assertEquals('hello', handler.logRecord.getMessage());
  112. assertEquals(ex, handler.logRecord.getException());
  113. }
  114. function testMessageCallbacks() {
  115. var root = goog.debug.LogManager.getRoot();
  116. var handler = new TestHandler_();
  117. var f = goog.bind(handler.onPublish, handler);
  118. root.addHandler(f);
  119. var logger = goog.log.getLogger('goog.bar.foo');
  120. logger.setLevel(goog.log.Level.WARNING);
  121. logger.log(goog.log.Level.INFO, function() {
  122. throw "Message callback shouldn't be called when below logger's level!";
  123. });
  124. assertNull(handler.logRecord);
  125. logger.log(goog.log.Level.WARNING, function() { return 'heya' });
  126. assertNotNull(handler.logRecord);
  127. assertEquals(goog.log.Level.WARNING, handler.logRecord.getLevel());
  128. assertEquals('heya', handler.logRecord.getMessage());
  129. }
  130. function testGetLogRecord() {
  131. var name = 'test.get.log.record';
  132. var level = goog.log.Level.FINE;
  133. var msg = 'msg';
  134. var logger = goog.log.getLogger(name);
  135. var logRecord = logger.getLogRecord(level, msg);
  136. assertEquals(name, logRecord.getLoggerName());
  137. assertEquals(level, logRecord.getLevel());
  138. assertEquals(msg, logRecord.getMessage());
  139. assertNull(logRecord.getException());
  140. }
  141. function testGetLogRecordWithException() {
  142. var name = 'test.get.log.record';
  143. var level = goog.log.Level.FINE;
  144. var msg = 'msg';
  145. var ex = Error('Hi');
  146. var logger = goog.log.getLogger(name);
  147. var logRecord = logger.getLogRecord(level, msg, ex);
  148. assertEquals(name, logRecord.getLoggerName());
  149. assertEquals(level, logRecord.getLevel());
  150. assertEquals(msg, logRecord.getMessage());
  151. assertEquals(ex, logRecord.getException());
  152. }