Skip to content

Commit

Permalink
Merge replaceLoggingMethods() and rebuildLoggingMethods()
Browse files Browse the repository at this point in the history
Keeping these separate was unnecessarily confusing.
  • Loading branch information
Mr0grog committed Jan 22, 2024
1 parent 4cd061f commit 4f15c59
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions lib/loglevel.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,33 +86,41 @@

// These private functions always need `this` to be set properly

function replaceLoggingMethods(level, loggerName) {
function replaceLoggingMethods() {
/*jshint validthis:true */
var level = this.getLevel();

// Replace the actual methods.
for (var i = 0; i < logMethods.length; i++) {
var methodName = logMethods[i];
this[methodName] = (i < level) ?
noop :
this.methodFactory(methodName, level, loggerName);
this.methodFactory(methodName, level, this.name);
}

// Define log.log as an alias for log.debug
this.log = this.debug;

// Return any important warnings.
if (typeof console === undefinedType && level < this.levels.SILENT) {
return "No console available for logging";
}
}

// In old IE versions, the console isn't present until you first open it.
// We build realMethod() replacements here that regenerate logging methods
function enableLoggingWhenConsoleArrives(methodName, level, loggerName) {
function enableLoggingWhenConsoleArrives(methodName) {
return function () {
if (typeof console !== undefinedType) {
replaceLoggingMethods.call(this, level, loggerName);
replaceLoggingMethods.call(this);
this[methodName].apply(this, arguments);
}
};
}

// By default, we use closely bound real methods wherever possible, and
// otherwise we wait for a console to appear, and then try again.
function defaultMethodFactory(methodName, level, loggerName) {
function defaultMethodFactory(methodName, _level, _loggerName) {
/*jshint validthis:true */
return realMethod(methodName) ||
enableLoggingWhenConsoleArrives.apply(this, arguments);
Expand Down Expand Up @@ -226,15 +234,6 @@
}
}

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 Down Expand Up @@ -265,7 +264,7 @@
}

// NOTE: in v2, this should call rebuild(), which updates children.
return rebuildLoggingMethods();
return replaceLoggingMethods.call(this);
};

self.setDefaultLevel = function (level) {
Expand All @@ -278,7 +277,7 @@
self.resetLevel = function () {
userLevel = null;
clearPersistedLevel();
rebuildLoggingMethods();
replaceLoggingMethods.call(this);
};

self.enableAll = function(persist) {
Expand All @@ -293,7 +292,7 @@
if (defaultLogger !== self) {
inheritedLevel = normalizeLevel(defaultLogger.getLevel());
}
rebuildLoggingMethods();
replaceLoggingMethods.call(this);

if (defaultLogger === self) {
for (var childName in _loggersByName) {
Expand All @@ -313,7 +312,7 @@
if (initialLevel != null) {
userLevel = normalizeLevel(initialLevel);
}
rebuildLoggingMethods();
replaceLoggingMethods.call(this);
}

/*
Expand Down

0 comments on commit 4f15c59

Please sign in to comment.