From 5d5f6695a21945de0444ce0e4a6f9275de49292c Mon Sep 17 00:00:00 2001 From: Tim Perry Date: Fri, 26 Jan 2024 00:53:37 +0100 Subject: [PATCH] Fix bug when loglevel is used with some ESM translation (e.g. ts-node) Although loglevel itself is not published using ESM, it seems that some runtime environments will treat its import as such. Because of this, when it's imported with syntax like "import * as log ...", all fields of loglevel are treated as ESM exports, meaning they're frozen and read-only. This is problematic, because loglevel rewrites those fields to change levels, which now fails. This happened in v1.9.0 because we switched from replacing log methods on 'self' to 'this'. 'self' always referred to the logger object within our environment, but 'this' in these runtime environments seems to point to the ESM-imported wrapper which is unmodifiable. Using the internal self reference to our internal state seems to work correctly instead, with no downsides, so we're reverting to that. --- lib/loglevel.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/loglevel.js b/lib/loglevel.js index 469069e..307ab2f 100644 --- a/lib/loglevel.js +++ b/lib/loglevel.js @@ -264,7 +264,7 @@ } // NOTE: in v2, this should call rebuild(), which updates children. - return replaceLoggingMethods.call(this); + return replaceLoggingMethods.call(self); }; self.setDefaultLevel = function (level) { @@ -277,7 +277,7 @@ self.resetLevel = function () { userLevel = null; clearPersistedLevel(); - replaceLoggingMethods.call(this); + replaceLoggingMethods.call(self); }; self.enableAll = function(persist) { @@ -292,7 +292,7 @@ if (defaultLogger !== self) { inheritedLevel = normalizeLevel(defaultLogger.getLevel()); } - replaceLoggingMethods.call(this); + replaceLoggingMethods.call(self); if (defaultLogger === self) { for (var childName in _loggersByName) { @@ -309,7 +309,7 @@ if (initialLevel != null) { userLevel = normalizeLevel(initialLevel); } - replaceLoggingMethods.call(this); + replaceLoggingMethods.call(self); } /*