Skip to content

Commit

Permalink
Fix bug when loglevel is used with some ESM translation (e.g. ts-node)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pimterry committed Jan 25, 2024
1 parent 118c2ae commit 5d5f669
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/loglevel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -277,7 +277,7 @@
self.resetLevel = function () {
userLevel = null;
clearPersistedLevel();
replaceLoggingMethods.call(this);
replaceLoggingMethods.call(self);
};

self.enableAll = function(persist) {
Expand All @@ -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) {
Expand All @@ -309,7 +309,7 @@
if (initialLevel != null) {
userLevel = normalizeLevel(initialLevel);
}
replaceLoggingMethods.call(this);
replaceLoggingMethods.call(self);
}

/*
Expand Down

0 comments on commit 5d5f669

Please sign in to comment.