3 define(['test/test-helpers'], function(testHelpers) {
4 var describeIf = testHelpers.describeIf;
5 var it = testHelpers.itWithFreshLog;
7 var originalConsole = window.console;
9 describe("Multiple logger instances tests:", function() {
11 describe("log.getLogger()", function() {
12 it("returns a new logger that is not the default one", function(log) {
13 var newLogger = log.getLogger("newLogger");
14 expect(newLogger).not.toEqual(log);
15 expect(newLogger.trace).toBeDefined();
16 expect(newLogger.debug).toBeDefined();
17 expect(newLogger.info).toBeDefined();
18 expect(newLogger.warn).toBeDefined();
19 expect(newLogger.error).toBeDefined();
20 expect(newLogger.setLevel).toBeDefined();
21 expect(newLogger.setDefaultLevel).toBeDefined();
22 expect(newLogger.enableAll).toBeDefined();
23 expect(newLogger.disableAll).toBeDefined();
24 expect(newLogger.methodFactory).toBeDefined();
27 it("returns loggers without `getLogger()` and `noConflict()`", function(log) {
28 var newLogger = log.getLogger("newLogger");
29 expect(newLogger.getLogger).toBeUndefined();
30 expect(newLogger.noConflict).toBeUndefined();
33 it("returns the same instance when called repeatedly with the same name", function(log) {
34 var logger1 = log.getLogger("newLogger");
35 var logger2 = log.getLogger("newLogger");
37 expect(logger1).toEqual(logger2);
40 it("should throw if called with no name", function(log) {
46 it("should throw if called with empty string for name", function(log) {
52 it("should throw if called with a non-string name", function(log) {
53 expect(function() { log.getLogger(true); }).toThrow();
54 expect(function() { log.getLogger({}); }).toThrow();
55 expect(function() { log.getLogger([]); }).toThrow();
56 expect(function() { log.getLogger(10); }).toThrow();
57 expect(function() { log.getLogger(function(){}); }).toThrow();
58 expect(function() { log.getLogger(null); }).toThrow();
59 expect(function() { log.getLogger(undefined); }).toThrow();
61 expect(function() { log.getLogger(Symbol()); }).toThrow();
66 describe("inheritance", function() {
67 beforeEach(function() {
68 window.console = {"log" : jasmine.createSpy("console.log")};
70 "toBeAtLevel" : testHelpers.toBeAtLevel
72 testHelpers.clearStoredLevels();
75 afterEach(function() {
76 window.console = originalConsole;
79 it("loggers are created with the same level as the default logger", function(log) {
80 log.setLevel("ERROR");
81 var newLogger = log.getLogger("newLogger");
82 expect(newLogger).toBeAtLevel("error");
85 it("if a logger's level is persisted, it uses that level rather than the default logger's level", function(log) {
86 testHelpers.setStoredLevel("error", "newLogger");
87 log.setLevel("TRACE");
88 var newLogger = log.getLogger("newLogger");
89 expect(newLogger).toBeAtLevel("error");
92 it("other loggers do not change when the default logger's level is changed", function(log) {
93 log.setLevel("TRACE");
94 var newLogger = log.getLogger("newLogger");
95 log.setLevel("ERROR");
96 expect(newLogger).toBeAtLevel("TRACE");
97 expect(log.getLogger("newLogger")).toBeAtLevel("TRACE");
100 it("loggers are created with the same methodFactory as the default logger", function(log) {
101 log.methodFactory = function(methodName, level) {
102 return function() {};
105 var newLogger = log.getLogger("newLogger");
106 expect(newLogger.methodFactory).toEqual(log.methodFactory);
109 it("loggers have independent method factories", function(log) {
110 var log1 = log.getLogger('logger1');
111 var log2 = log.getLogger('logger2');
113 var log1Spy = jasmine.createSpy('log1spy');
114 log1.methodFactory = function(methodName, level) {
117 log1.setLevel(log1.getLevel());
119 var log2Spy = jasmine.createSpy('log2spy');
120 log2.methodFactory = function(methodName, level) {
123 log2.setLevel(log2.getLevel());
128 expect(log1Spy).toHaveBeenCalledWith("test1");
129 expect(log2Spy).toHaveBeenCalledWith("test2");
132 it("new loggers correctly inherit a logging level of `0`", function(log) {
134 var newLogger = log.getLogger("newLogger");
135 expect(newLogger).toBeAtLevel("trace");