diff --git a/lib/loglevel.js b/lib/loglevel.js index 1c3636d..48948e5 100644 --- a/lib/loglevel.js +++ b/lib/loglevel.js @@ -205,6 +205,15 @@ } } + function rebuildLoggingMethods() { + var level = self.getLevel(); + + replaceLoggingMethods.call(self, level, name); + if (typeof console === undefinedType && level < self.levels.SILENT) { + return "No console available for logging"; + } + } + /* * * Public logger API - see https://github.com/pimterry/loglevel for details @@ -228,13 +237,15 @@ if (persist !== false) { // defaults to true persistLevelIfPossible(level); } - return self.rebuild(); + // NOTE: in v2, this should call rebuild(), which is recursive. + return rebuildLoggingMethods(); }; self.setDefaultLevel = function (level) { defaultLevel = normalizeLevel(level); if (currentLevel == null) { - self.rebuild(); + // NOTE: in v2, this should call rebuild(), which is recursive. + rebuildLoggingMethods(); } }; @@ -252,20 +263,14 @@ self.setLevel(self.levels.SILENT, persist); }; - self.rebuild = function (includeChildren) { - // NOTE: includeChildren should default to `true` in v2. - includeChildren = !!includeChildren; - var level = self.getLevel(); + self.rebuild = function () { + rebuildLoggingMethods(); - replaceLoggingMethods.call(self, level, name); - if (typeof console === undefinedType && level < self.levels.SILENT) { - return "No console available for logging"; - } + if (defaultLogger === self) { + var level = self.getLevel(); - if (includeChildren && defaultLogger === self) { for (var childName in _loggersByName) { - var child = _loggersByName[childName]; - child.setDefaultLevel(level); + _loggersByName[childName].setDefaultLevel(level); } } }; diff --git a/test/multiple-logger-test.js b/test/multiple-logger-test.js index 734a774..9895f6f 100644 --- a/test/multiple-logger-test.js +++ b/test/multiple-logger-test.js @@ -134,8 +134,22 @@ define(['test/test-helpers'], function(testHelpers) { var newLogger = log.getLogger("newLogger"); expect(newLogger).toBeAtLevel("trace"); }); + }); + + describe("logger.rebuild()", function() { + beforeEach(function() { + window.console = {"log" : jasmine.createSpy("console.log")}; + jasmine.addMatchers({ + "toBeAtLevel" : testHelpers.toBeAtLevel + }); + testHelpers.clearStoredLevels(); + }); + + afterEach(function() { + window.console = originalConsole; + }); - it("rebuilds existing child loggers via root.rebuild(children=true)", function(log) { + it("rebuilds existing child loggers", function(log) { log.setLevel("TRACE"); var newLogger = log.getLogger("newLogger"); expect(newLogger).toBeAtLevel("TRACE"); @@ -147,7 +161,7 @@ define(['test/test-helpers'], function(testHelpers) { expect(newLogger).toBeAtLevel("ERROR"); }); - it("should not change a child's persisted level when calling root.rebuild(children=true)", function(log) { + it("should not change a child's persisted level", function(log) { testHelpers.setStoredLevel("ERROR", "newLogger"); log.setLevel("TRACE"); @@ -158,7 +172,7 @@ define(['test/test-helpers'], function(testHelpers) { expect(newLogger).toBeAtLevel("ERROR"); }); - it("should not change a child's explicitly set level even if un-persisted when calling root.rebuild(children=true)", function(log) { + it("should not change a child's explicitly set level even if un-persisted", function(log) { log.setLevel("TRACE"); var newLogger = log.getLogger("newLogger"); newLogger.setLevel("DEBUG", false);