Skip to content

Commit e7503a4

Browse files
refactor: further logger clean-up (#4050)
After #4049 here are two small follow-up improvements to `js/logger.js`. **1. Simpler bind syntax** — `Function.prototype.bind.call(console.debug, console)` is an archaic pattern. The equivalent `console.debug.bind(console)` works fine in all supported engines (Node.js ≥ 22, modern browsers) and is much easier to read. Also: `console.timeStamp` exists in all supported environments, so the conditional fallback to an empty function is no longer needed. **2. Simpler `setLogLevel`** — instead of iterating over all keys in the logger object and permanently overwriting them, the method now loops over the five log-level keys explicitly and rebinds from `console[key]`. This makes the filtered set obvious at a glance and ensures utility methods like `group`, `time`, and `timeStamp` are never accidentally silenced — they're structural helpers, not log levels.
1 parent 3eb3745 commit e7503a4

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

js/logger.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,26 +70,25 @@
7070

7171
if (enableLog) {
7272
logLevel = {
73-
debug: Function.prototype.bind.call(console.debug, console),
74-
log: Function.prototype.bind.call(console.log, console),
75-
info: Function.prototype.bind.call(console.info, console),
76-
warn: Function.prototype.bind.call(console.warn, console),
77-
error: Function.prototype.bind.call(console.error, console),
78-
group: Function.prototype.bind.call(console.group, console),
79-
groupCollapsed: Function.prototype.bind.call(console.groupCollapsed, console),
80-
groupEnd: Function.prototype.bind.call(console.groupEnd, console),
81-
time: Function.prototype.bind.call(console.time, console),
82-
timeEnd: Function.prototype.bind.call(console.timeEnd, console),
83-
timeStamp: console.timeStamp ? Function.prototype.bind.call(console.timeStamp, console) : function () {}
73+
debug: console.debug.bind(console),
74+
log: console.log.bind(console),
75+
info: console.info.bind(console),
76+
warn: console.warn.bind(console),
77+
error: console.error.bind(console),
78+
group: console.group.bind(console),
79+
groupCollapsed: console.groupCollapsed.bind(console),
80+
groupEnd: console.groupEnd.bind(console),
81+
time: console.time.bind(console),
82+
timeEnd: console.timeEnd.bind(console),
83+
timeStamp: console.timeStamp.bind(console)
8484
};
8585

86+
// Only these methods are affected by setLogLevel.
87+
// Utility methods (group, time, etc.) are always active.
8688
logLevel.setLogLevel = function (newLevel) {
87-
if (newLevel) {
88-
Object.keys(logLevel).forEach(function (key) {
89-
if (!newLevel.includes(key.toLocaleUpperCase())) {
90-
logLevel[key] = function () {};
91-
}
92-
});
89+
for (const key of ["debug", "log", "info", "warn", "error"]) {
90+
const disabled = newLevel && !newLevel.includes(key.toUpperCase());
91+
logLevel[key] = disabled ? function () {} : console[key].bind(console);
9392
}
9493
};
9594
} else {

0 commit comments

Comments
 (0)