Skip to content

Commit

Permalink
Make logger.rebuild() always recursive
Browse files Browse the repository at this point in the history
This moves the non-recursive logic into a private method and makes the
public `logger.rebuild()` method always recursive, per PR review.
  • Loading branch information
Mr0grog committed Jan 10, 2024
1 parent e4f91a9 commit 4539639
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
31 changes: 18 additions & 13 deletions lib/loglevel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
};

Expand All @@ -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);
}
}
};
Expand Down
20 changes: 17 additions & 3 deletions test/multiple-logger-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -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);
Expand Down

0 comments on commit 4539639

Please sign in to comment.