mirror of
https://github.com/MichMich/MagicMirror.git
synced 2026-04-21 21:40:55 +00:00
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.
This commit is contained in:
committed by
GitHub
parent
3eb3745dd3
commit
e7503a457b
33
js/logger.js
33
js/logger.js
@@ -70,26 +70,25 @@
|
||||
|
||||
if (enableLog) {
|
||||
logLevel = {
|
||||
debug: Function.prototype.bind.call(console.debug, console),
|
||||
log: Function.prototype.bind.call(console.log, console),
|
||||
info: Function.prototype.bind.call(console.info, console),
|
||||
warn: Function.prototype.bind.call(console.warn, console),
|
||||
error: Function.prototype.bind.call(console.error, console),
|
||||
group: Function.prototype.bind.call(console.group, console),
|
||||
groupCollapsed: Function.prototype.bind.call(console.groupCollapsed, console),
|
||||
groupEnd: Function.prototype.bind.call(console.groupEnd, console),
|
||||
time: Function.prototype.bind.call(console.time, console),
|
||||
timeEnd: Function.prototype.bind.call(console.timeEnd, console),
|
||||
timeStamp: console.timeStamp ? Function.prototype.bind.call(console.timeStamp, console) : function () {}
|
||||
debug: console.debug.bind(console),
|
||||
log: console.log.bind(console),
|
||||
info: console.info.bind(console),
|
||||
warn: console.warn.bind(console),
|
||||
error: console.error.bind(console),
|
||||
group: console.group.bind(console),
|
||||
groupCollapsed: console.groupCollapsed.bind(console),
|
||||
groupEnd: console.groupEnd.bind(console),
|
||||
time: console.time.bind(console),
|
||||
timeEnd: console.timeEnd.bind(console),
|
||||
timeStamp: console.timeStamp.bind(console)
|
||||
};
|
||||
|
||||
// Only these methods are affected by setLogLevel.
|
||||
// Utility methods (group, time, etc.) are always active.
|
||||
logLevel.setLogLevel = function (newLevel) {
|
||||
if (newLevel) {
|
||||
Object.keys(logLevel).forEach(function (key) {
|
||||
if (!newLevel.includes(key.toLocaleUpperCase())) {
|
||||
logLevel[key] = function () {};
|
||||
}
|
||||
});
|
||||
for (const key of ["debug", "log", "info", "warn", "error"]) {
|
||||
const disabled = newLevel && !newLevel.includes(key.toUpperCase());
|
||||
logLevel[key] = disabled ? function () {} : console[key].bind(console);
|
||||
}
|
||||
};
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user