From 4f15c598959af1d5cd173f426f8105a8993acf11 Mon Sep 17 00:00:00 2001 From: Rob Brackett Date: Mon, 22 Jan 2024 23:38:13 +0000 Subject: [PATCH] Merge `replaceLoggingMethods()` and `rebuildLoggingMethods()` Keeping these separate was unnecessarily confusing. --- lib/loglevel.js | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/lib/loglevel.js b/lib/loglevel.js index 604cc21..4429177 100644 --- a/lib/loglevel.js +++ b/lib/loglevel.js @@ -86,25 +86,33 @@ // 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); } }; @@ -112,7 +120,7 @@ // 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); @@ -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 @@ -265,7 +264,7 @@ } // NOTE: in v2, this should call rebuild(), which updates children. - return rebuildLoggingMethods(); + return replaceLoggingMethods.call(this); }; self.setDefaultLevel = function (level) { @@ -278,7 +277,7 @@ self.resetLevel = function () { userLevel = null; clearPersistedLevel(); - rebuildLoggingMethods(); + replaceLoggingMethods.call(this); }; self.enableAll = function(persist) { @@ -293,7 +292,7 @@ if (defaultLogger !== self) { inheritedLevel = normalizeLevel(defaultLogger.getLevel()); } - rebuildLoggingMethods(); + replaceLoggingMethods.call(this); if (defaultLogger === self) { for (var childName in _loggersByName) { @@ -313,7 +312,7 @@ if (initialLevel != null) { userLevel = normalizeLevel(initialLevel); } - rebuildLoggingMethods(); + replaceLoggingMethods.call(this); } /*