logger_test.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. goog.provide('goog.debug.LoggerTest');
  15. goog.setTestOnly('goog.debug.LoggerTest');
  16. goog.require('goog.debug.LogManager');
  17. goog.require('goog.debug.Logger');
  18. goog.require('goog.testing.jsunit');
  19. function getDebug(sb, logger, level) {
  20. var spacer = '';
  21. if (level) {
  22. spacer = (new Array(level + 1)).join(' ');
  23. }
  24. sb[sb.length] = spacer;
  25. var name = logger.getName();
  26. if (name) {
  27. sb[sb.length] = name;
  28. } else {
  29. sb[sb.length] = 'ROOT';
  30. }
  31. sb[sb.length] = '\n';
  32. var children = logger.getChildren();
  33. for (var key in children) {
  34. getDebug(sb, children[key], level + 1);
  35. }
  36. }
  37. function testParents() {
  38. var l1 = goog.debug.Logger.getLogger('goog.test');
  39. var l2 = goog.debug.Logger.getLogger('goog.bar');
  40. var l3 = goog.debug.Logger.getLogger('goog.bar.foo');
  41. var l4 = goog.debug.Logger.getLogger('goog.bar.baaz');
  42. var rootLogger = goog.debug.LogManager.getRoot();
  43. var googLogger = goog.debug.Logger.getLogger('goog');
  44. assertEquals(rootLogger, googLogger.getParent());
  45. assertEquals(googLogger, l1.getParent());
  46. assertEquals(googLogger, l2.getParent());
  47. assertEquals(l2, l3.getParent());
  48. assertEquals(l2, l4.getParent());
  49. }
  50. function testLogging() {
  51. var root = goog.debug.LogManager.getRoot();
  52. var handler = new TestHandler();
  53. var f = goog.bind(handler.onPublish, handler);
  54. root.addHandler(f);
  55. var l4 = goog.debug.Logger.getLogger('goog.bar.baaz');
  56. l4.log(goog.debug.Logger.Level.WARNING, 'foo');
  57. assertNotNull(handler.logRecord);
  58. assertEquals(goog.debug.Logger.Level.WARNING, handler.logRecord.getLevel());
  59. assertEquals('foo', handler.logRecord.getMessage());
  60. handler.logRecord = null;
  61. root.removeHandler(f);
  62. l4.log(goog.debug.Logger.Level.WARNING, 'foo');
  63. assertNull(handler.logRecord);
  64. }
  65. function testFiltering() {
  66. var root = goog.debug.LogManager.getRoot();
  67. var handler = new TestHandler();
  68. var f = goog.bind(handler.onPublish, handler);
  69. root.addHandler(f);
  70. var l3 = goog.debug.Logger.getLogger('goog.bar.foo');
  71. l3.setLevel(goog.debug.Logger.Level.WARNING);
  72. var l4 = goog.debug.Logger.getLogger('goog.bar.baaz');
  73. l4.setLevel(goog.debug.Logger.Level.INFO);
  74. l4.log(goog.debug.Logger.Level.WARNING, 'foo');
  75. assertNotNull(handler.logRecord);
  76. assertEquals(goog.debug.Logger.Level.WARNING, handler.logRecord.getLevel());
  77. assertEquals('foo', handler.logRecord.getMessage());
  78. handler.reset();
  79. l3.log(goog.debug.Logger.Level.INFO, 'bar');
  80. assertNull(handler.logRecord);
  81. l3.log(goog.debug.Logger.Level.WARNING, 'baaz');
  82. assertNotNull(handler.logRecord);
  83. handler.reset();
  84. l3.log(goog.debug.Logger.Level.SEVERE, 'baaz');
  85. assertNotNull(handler.logRecord);
  86. }
  87. function testException() {
  88. var root = goog.debug.LogManager.getRoot();
  89. var handler = new TestHandler();
  90. var f = goog.bind(handler.onPublish, handler);
  91. root.addHandler(f);
  92. var logger = goog.debug.Logger.getLogger('goog.debug.logger_test');
  93. var ex = Error('boo!');
  94. logger.severe('hello', ex);
  95. assertNotNull(handler.logRecord);
  96. assertEquals(goog.debug.Logger.Level.SEVERE, handler.logRecord.getLevel());
  97. assertEquals('hello', handler.logRecord.getMessage());
  98. assertEquals(ex, handler.logRecord.getException());
  99. assertEquals('boo!', handler.logRecord.getException().message);
  100. }
  101. function testMessageCallbacks() {
  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 l3 = goog.debug.Logger.getLogger('goog.bar.foo');
  107. l3.setLevel(goog.debug.Logger.Level.WARNING);
  108. l3.log(goog.debug.Logger.Level.INFO, function() {
  109. throw "Message callback shouldn't be called when below logger's level!";
  110. });
  111. assertNull(handler.logRecord);
  112. l3.log(goog.debug.Logger.Level.WARNING, function() { return 'heya' });
  113. assertNotNull(handler.logRecord);
  114. assertEquals(goog.debug.Logger.Level.WARNING, handler.logRecord.getLevel());
  115. assertEquals('heya', handler.logRecord.getMessage());
  116. }
  117. function testGetPredefinedLevel() {
  118. assertEquals(
  119. goog.debug.Logger.Level.OFF,
  120. goog.debug.Logger.Level.getPredefinedLevel('OFF'));
  121. assertEquals(
  122. goog.debug.Logger.Level.SHOUT,
  123. goog.debug.Logger.Level.getPredefinedLevel('SHOUT'));
  124. assertEquals(
  125. goog.debug.Logger.Level.SEVERE,
  126. goog.debug.Logger.Level.getPredefinedLevel('SEVERE'));
  127. assertEquals(
  128. goog.debug.Logger.Level.WARNING,
  129. goog.debug.Logger.Level.getPredefinedLevel('WARNING'));
  130. assertEquals(
  131. goog.debug.Logger.Level.INFO,
  132. goog.debug.Logger.Level.getPredefinedLevel('INFO'));
  133. assertEquals(
  134. goog.debug.Logger.Level.CONFIG,
  135. goog.debug.Logger.Level.getPredefinedLevel('CONFIG'));
  136. assertEquals(
  137. goog.debug.Logger.Level.FINE,
  138. goog.debug.Logger.Level.getPredefinedLevel('FINE'));
  139. assertEquals(
  140. goog.debug.Logger.Level.FINER,
  141. goog.debug.Logger.Level.getPredefinedLevel('FINER'));
  142. assertEquals(
  143. goog.debug.Logger.Level.FINEST,
  144. goog.debug.Logger.Level.getPredefinedLevel('FINEST'));
  145. assertEquals(
  146. goog.debug.Logger.Level.ALL,
  147. goog.debug.Logger.Level.getPredefinedLevel('ALL'));
  148. }
  149. function testGetPredefinedLevelByValue() {
  150. assertEquals(
  151. goog.debug.Logger.Level.OFF,
  152. goog.debug.Logger.Level.getPredefinedLevelByValue(Infinity));
  153. assertEquals(
  154. goog.debug.Logger.Level.SHOUT,
  155. goog.debug.Logger.Level.getPredefinedLevelByValue(1300));
  156. assertEquals(
  157. goog.debug.Logger.Level.SHOUT,
  158. goog.debug.Logger.Level.getPredefinedLevelByValue(1200));
  159. assertEquals(
  160. goog.debug.Logger.Level.SEVERE,
  161. goog.debug.Logger.Level.getPredefinedLevelByValue(1150));
  162. assertEquals(
  163. goog.debug.Logger.Level.SEVERE,
  164. goog.debug.Logger.Level.getPredefinedLevelByValue(1000));
  165. assertEquals(
  166. goog.debug.Logger.Level.WARNING,
  167. goog.debug.Logger.Level.getPredefinedLevelByValue(900));
  168. assertEquals(
  169. goog.debug.Logger.Level.INFO,
  170. goog.debug.Logger.Level.getPredefinedLevelByValue(800));
  171. assertEquals(
  172. goog.debug.Logger.Level.CONFIG,
  173. goog.debug.Logger.Level.getPredefinedLevelByValue(701));
  174. assertEquals(
  175. goog.debug.Logger.Level.CONFIG,
  176. goog.debug.Logger.Level.getPredefinedLevelByValue(700));
  177. assertEquals(
  178. goog.debug.Logger.Level.FINE,
  179. goog.debug.Logger.Level.getPredefinedLevelByValue(500));
  180. assertEquals(
  181. goog.debug.Logger.Level.FINER,
  182. goog.debug.Logger.Level.getPredefinedLevelByValue(400));
  183. assertEquals(
  184. goog.debug.Logger.Level.FINEST,
  185. goog.debug.Logger.Level.getPredefinedLevelByValue(300));
  186. assertEquals(
  187. goog.debug.Logger.Level.ALL,
  188. goog.debug.Logger.Level.getPredefinedLevelByValue(0));
  189. assertNull(goog.debug.Logger.Level.getPredefinedLevelByValue(-1));
  190. }
  191. function TestHandler() {
  192. this.logRecord = null;
  193. }
  194. TestHandler.prototype.onPublish = function(logRecord) {
  195. this.logRecord = logRecord;
  196. };
  197. TestHandler.prototype.reset = function() {
  198. this.logRecord = null;
  199. };
  200. function testGetLogRecord() {
  201. var name = 'test.get.log.record';
  202. var level = 1;
  203. var msg = 'msg';
  204. var logger = goog.debug.Logger.getLogger(name);
  205. var logRecord = logger.getLogRecord(level, msg);
  206. assertEquals(name, logRecord.getLoggerName());
  207. assertEquals(level, logRecord.getLevel());
  208. assertEquals(msg, logRecord.getMessage());
  209. assertNull(logRecord.getException());
  210. }
  211. function testGetLogRecordWithException() {
  212. var name = 'test.get.log.record';
  213. var level = 1;
  214. var msg = 'msg';
  215. var ex = Error('Hi');
  216. var logger = goog.debug.Logger.getLogger(name);
  217. var logRecord = logger.getLogRecord(level, msg, ex);
  218. assertEquals(name, logRecord.getLoggerName());
  219. assertEquals(level, logRecord.getLevel());
  220. assertEquals(msg, logRecord.getMessage());
  221. assertEquals(ex, logRecord.getException());
  222. }