Files
MagicMirror/js/logger.js

115 lines
3.8 KiB
JavaScript
Raw Permalink Normal View History

Fix Node.js v25 logging prefix and modernize logger (#4049) On Node.js v25, the log prefix in the terminal stopped working - instead of seeing something like: ``` [2026-03-05 23:00:00.000] [LOG] [app] Starting MagicMirror: v2.35.0 ``` the output was: ``` [2026-03-05 23:00:00.000] :pre() Starting MagicMirror: v2.35.0 ``` Reported in #4048. ## Why did it break? The logger used the `console-stamp` package to format log output. One part of that formatting used `styleText("grey", ...)` to color the caller prefix gray. Node.js v25 dropped `"grey"` as a valid color name (only `"gray"` with an "a" is accepted now). This caused `styleText` to throw an error internally - and `console-stamp` silently swallowed that error and fell back to returning its raw `:pre()` format string as the prefix. Not ideal. ## What's in this PR? **1. The actual fix** - `"grey"` → `"gray"`. **2. Cleaner stack trace approach** - the previous code set `Error.prepareStackTrace` *after* creating the `Error`, which is fragile and was starting to behave differently across Node versions. Replaced with straightforward string parsing of `new Error().stack`. **3. Removed the `console-stamp` dependency** - all formatting is now done with plain Node.js built-ins (`node:util` `styleText`). Same visual result, no external dependency. **4. Simplified the module wrapper** - the logger was wrapped in a UMD pattern, which is meant for environments like AMD/RequireJS. MagicMirror only runs in two places: Node.js and the browser. Replaced with a simple check (`typeof module !== "undefined"`), which is much easier to follow.
2026-03-06 13:10:59 +01:00
// Logger for MagicMirror² — works both in Node.js (CommonJS) and the browser (global).
(function () {
if (typeof module !== "undefined") {
if (process.env.mmTestMode !== "true") {
const { styleText } = require("node:util");
Fix Node.js v25 logging prefix and modernize logger (#4049) On Node.js v25, the log prefix in the terminal stopped working - instead of seeing something like: ``` [2026-03-05 23:00:00.000] [LOG] [app] Starting MagicMirror: v2.35.0 ``` the output was: ``` [2026-03-05 23:00:00.000] :pre() Starting MagicMirror: v2.35.0 ``` Reported in #4048. ## Why did it break? The logger used the `console-stamp` package to format log output. One part of that formatting used `styleText("grey", ...)` to color the caller prefix gray. Node.js v25 dropped `"grey"` as a valid color name (only `"gray"` with an "a" is accepted now). This caused `styleText` to throw an error internally - and `console-stamp` silently swallowed that error and fell back to returning its raw `:pre()` format string as the prefix. Not ideal. ## What's in this PR? **1. The actual fix** - `"grey"` → `"gray"`. **2. Cleaner stack trace approach** - the previous code set `Error.prepareStackTrace` *after* creating the `Error`, which is fragile and was starting to behave differently across Node versions. Replaced with straightforward string parsing of `new Error().stack`. **3. Removed the `console-stamp` dependency** - all formatting is now done with plain Node.js built-ins (`node:util` `styleText`). Same visual result, no external dependency. **4. Simplified the module wrapper** - the logger was wrapped in a UMD pattern, which is meant for environments like AMD/RequireJS. MagicMirror only runs in two places: Node.js and the browser. Replaced with a simple check (`typeof module !== "undefined"`), which is much easier to follow.
2026-03-06 13:10:59 +01:00
const LABEL_COLORS = { error: "red", warn: "yellow", debug: "bgBlue", info: "blue" };
const MSG_COLORS = { error: "red", warn: "yellow", info: "blue" };
const formatTimestamp = () => {
const d = new Date();
const pad2 = (n) => String(n).padStart(2, "0");
const pad3 = (n) => String(n).padStart(3, "0");
const date = `${d.getFullYear()}-${pad2(d.getMonth() + 1)}-${pad2(d.getDate())}`;
const time = `${pad2(d.getHours())}:${pad2(d.getMinutes())}:${pad2(d.getSeconds())}.${pad3(d.getMilliseconds())}`;
return `[${date} ${time}]`;
};
const getCallerPrefix = () => {
try {
const lines = new Error().stack.split("\n");
for (const line of lines) {
if (line.includes("node:") || line.includes("js/logger.js") || line.includes("node_modules")) continue;
const match = line.match(/\((.+?\.js):\d+:\d+\)/) || line.match(/at\s+(.+?\.js):\d+:\d+/);
if (match) {
const file = match[1];
const baseName = file.replace(/.*\/(.*)\.js/, "$1");
const parentDir = file.replace(/.*\/(.*)\/.*\.js/, "$1");
return styleText("gray", parentDir === "js" ? `[${baseName}]` : `[${parentDir}]`);
}
}
} catch { /* ignore */ }
Fix Node.js v25 logging prefix and modernize logger (#4049) On Node.js v25, the log prefix in the terminal stopped working - instead of seeing something like: ``` [2026-03-05 23:00:00.000] [LOG] [app] Starting MagicMirror: v2.35.0 ``` the output was: ``` [2026-03-05 23:00:00.000] :pre() Starting MagicMirror: v2.35.0 ``` Reported in #4048. ## Why did it break? The logger used the `console-stamp` package to format log output. One part of that formatting used `styleText("grey", ...)` to color the caller prefix gray. Node.js v25 dropped `"grey"` as a valid color name (only `"gray"` with an "a" is accepted now). This caused `styleText` to throw an error internally - and `console-stamp` silently swallowed that error and fell back to returning its raw `:pre()` format string as the prefix. Not ideal. ## What's in this PR? **1. The actual fix** - `"grey"` → `"gray"`. **2. Cleaner stack trace approach** - the previous code set `Error.prepareStackTrace` *after* creating the `Error`, which is fragile and was starting to behave differently across Node versions. Replaced with straightforward string parsing of `new Error().stack`. **3. Removed the `console-stamp` dependency** - all formatting is now done with plain Node.js built-ins (`node:util` `styleText`). Same visual result, no external dependency. **4. Simplified the module wrapper** - the logger was wrapped in a UMD pattern, which is meant for environments like AMD/RequireJS. MagicMirror only runs in two places: Node.js and the browser. Replaced with a simple check (`typeof module !== "undefined"`), which is much easier to follow.
2026-03-06 13:10:59 +01:00
return styleText("gray", "[unknown]");
};
// Patch console methods to prepend timestamp, level label, and caller prefix.
for (const method of ["debug", "log", "info", "warn", "error"]) {
const original = console[method].bind(console);
const labelRaw = `[${method.toUpperCase()}]`.padEnd(7);
const label = LABEL_COLORS[method] ? styleText(LABEL_COLORS[method], labelRaw) : labelRaw;
console[method] = (...args) => {
const prefix = `${formatTimestamp()} ${label} ${getCallerPrefix()}`;
const msgColor = MSG_COLORS[method];
if (msgColor && args.length > 0 && typeof args[0] === "string") {
original(prefix, styleText(msgColor, args[0]), ...args.slice(1));
} else {
original(prefix, ...args);
}
};
}
}
Fix Node.js v25 logging prefix and modernize logger (#4049) On Node.js v25, the log prefix in the terminal stopped working - instead of seeing something like: ``` [2026-03-05 23:00:00.000] [LOG] [app] Starting MagicMirror: v2.35.0 ``` the output was: ``` [2026-03-05 23:00:00.000] :pre() Starting MagicMirror: v2.35.0 ``` Reported in #4048. ## Why did it break? The logger used the `console-stamp` package to format log output. One part of that formatting used `styleText("grey", ...)` to color the caller prefix gray. Node.js v25 dropped `"grey"` as a valid color name (only `"gray"` with an "a" is accepted now). This caused `styleText` to throw an error internally - and `console-stamp` silently swallowed that error and fell back to returning its raw `:pre()` format string as the prefix. Not ideal. ## What's in this PR? **1. The actual fix** - `"grey"` → `"gray"`. **2. Cleaner stack trace approach** - the previous code set `Error.prepareStackTrace` *after* creating the `Error`, which is fragile and was starting to behave differently across Node versions. Replaced with straightforward string parsing of `new Error().stack`. **3. Removed the `console-stamp` dependency** - all formatting is now done with plain Node.js built-ins (`node:util` `styleText`). Same visual result, no external dependency. **4. Simplified the module wrapper** - the logger was wrapped in a UMD pattern, which is meant for environments like AMD/RequireJS. MagicMirror only runs in two places: Node.js and the browser. Replaced with a simple check (`typeof module !== "undefined"`), which is much easier to follow.
2026-03-06 13:10:59 +01:00
// Node, CommonJS
module.exports = makeLogger();
} else {
Fix Node.js v25 logging prefix and modernize logger (#4049) On Node.js v25, the log prefix in the terminal stopped working - instead of seeing something like: ``` [2026-03-05 23:00:00.000] [LOG] [app] Starting MagicMirror: v2.35.0 ``` the output was: ``` [2026-03-05 23:00:00.000] :pre() Starting MagicMirror: v2.35.0 ``` Reported in #4048. ## Why did it break? The logger used the `console-stamp` package to format log output. One part of that formatting used `styleText("grey", ...)` to color the caller prefix gray. Node.js v25 dropped `"grey"` as a valid color name (only `"gray"` with an "a" is accepted now). This caused `styleText` to throw an error internally - and `console-stamp` silently swallowed that error and fell back to returning its raw `:pre()` format string as the prefix. Not ideal. ## What's in this PR? **1. The actual fix** - `"grey"` → `"gray"`. **2. Cleaner stack trace approach** - the previous code set `Error.prepareStackTrace` *after* creating the `Error`, which is fragile and was starting to behave differently across Node versions. Replaced with straightforward string parsing of `new Error().stack`. **3. Removed the `console-stamp` dependency** - all formatting is now done with plain Node.js built-ins (`node:util` `styleText`). Same visual result, no external dependency. **4. Simplified the module wrapper** - the logger was wrapped in a UMD pattern, which is meant for environments like AMD/RequireJS. MagicMirror only runs in two places: Node.js and the browser. Replaced with a simple check (`typeof module !== "undefined"`), which is much easier to follow.
2026-03-06 13:10:59 +01:00
// Browser globals
window.Log = makeLogger();
}
Fix Node.js v25 logging prefix and modernize logger (#4049) On Node.js v25, the log prefix in the terminal stopped working - instead of seeing something like: ``` [2026-03-05 23:00:00.000] [LOG] [app] Starting MagicMirror: v2.35.0 ``` the output was: ``` [2026-03-05 23:00:00.000] :pre() Starting MagicMirror: v2.35.0 ``` Reported in #4048. ## Why did it break? The logger used the `console-stamp` package to format log output. One part of that formatting used `styleText("grey", ...)` to color the caller prefix gray. Node.js v25 dropped `"grey"` as a valid color name (only `"gray"` with an "a" is accepted now). This caused `styleText` to throw an error internally - and `console-stamp` silently swallowed that error and fell back to returning its raw `:pre()` format string as the prefix. Not ideal. ## What's in this PR? **1. The actual fix** - `"grey"` → `"gray"`. **2. Cleaner stack trace approach** - the previous code set `Error.prepareStackTrace` *after* creating the `Error`, which is fragile and was starting to behave differently across Node versions. Replaced with straightforward string parsing of `new Error().stack`. **3. Removed the `console-stamp` dependency** - all formatting is now done with plain Node.js built-ins (`node:util` `styleText`). Same visual result, no external dependency. **4. Simplified the module wrapper** - the logger was wrapped in a UMD pattern, which is meant for environments like AMD/RequireJS. MagicMirror only runs in two places: Node.js and the browser. Replaced with a simple check (`typeof module !== "undefined"`), which is much easier to follow.
2026-03-06 13:10:59 +01:00
/**
* Creates the logger object. Logging is disabled when running in test mode
* (Node.js) or inside jsdom (browser).
* @returns {object} The logger object with log level methods.
*/
function makeLogger () {
const enableLog = typeof module !== "undefined"
? process.env.mmTestMode !== "true"
: typeof window === "object" && window.name !== "jsdom";
2021-09-09 23:30:36 +02:00
Fix Node.js v25 logging prefix and modernize logger (#4049) On Node.js v25, the log prefix in the terminal stopped working - instead of seeing something like: ``` [2026-03-05 23:00:00.000] [LOG] [app] Starting MagicMirror: v2.35.0 ``` the output was: ``` [2026-03-05 23:00:00.000] :pre() Starting MagicMirror: v2.35.0 ``` Reported in #4048. ## Why did it break? The logger used the `console-stamp` package to format log output. One part of that formatting used `styleText("grey", ...)` to color the caller prefix gray. Node.js v25 dropped `"grey"` as a valid color name (only `"gray"` with an "a" is accepted now). This caused `styleText` to throw an error internally - and `console-stamp` silently swallowed that error and fell back to returning its raw `:pre()` format string as the prefix. Not ideal. ## What's in this PR? **1. The actual fix** - `"grey"` → `"gray"`. **2. Cleaner stack trace approach** - the previous code set `Error.prepareStackTrace` *after* creating the `Error`, which is fragile and was starting to behave differently across Node versions. Replaced with straightforward string parsing of `new Error().stack`. **3. Removed the `console-stamp` dependency** - all formatting is now done with plain Node.js built-ins (`node:util` `styleText`). Same visual result, no external dependency. **4. Simplified the module wrapper** - the logger was wrapped in a UMD pattern, which is meant for environments like AMD/RequireJS. MagicMirror only runs in two places: Node.js and the browser. Replaced with a simple check (`typeof module !== "undefined"`), which is much easier to follow.
2026-03-06 13:10:59 +01:00
let logLevel;
2020-05-11 07:46:56 +02:00
Fix Node.js v25 logging prefix and modernize logger (#4049) On Node.js v25, the log prefix in the terminal stopped working - instead of seeing something like: ``` [2026-03-05 23:00:00.000] [LOG] [app] Starting MagicMirror: v2.35.0 ``` the output was: ``` [2026-03-05 23:00:00.000] :pre() Starting MagicMirror: v2.35.0 ``` Reported in #4048. ## Why did it break? The logger used the `console-stamp` package to format log output. One part of that formatting used `styleText("grey", ...)` to color the caller prefix gray. Node.js v25 dropped `"grey"` as a valid color name (only `"gray"` with an "a" is accepted now). This caused `styleText` to throw an error internally - and `console-stamp` silently swallowed that error and fell back to returning its raw `:pre()` format string as the prefix. Not ideal. ## What's in this PR? **1. The actual fix** - `"grey"` → `"gray"`. **2. Cleaner stack trace approach** - the previous code set `Error.prepareStackTrace` *after* creating the `Error`, which is fragile and was starting to behave differently across Node versions. Replaced with straightforward string parsing of `new Error().stack`. **3. Removed the `console-stamp` dependency** - all formatting is now done with plain Node.js built-ins (`node:util` `styleText`). Same visual result, no external dependency. **4. Simplified the module wrapper** - the logger was wrapped in a UMD pattern, which is meant for environments like AMD/RequireJS. MagicMirror only runs in two places: Node.js and the browser. Replaced with a simple check (`typeof module !== "undefined"`), which is much easier to follow.
2026-03-06 13:10:59 +01:00
if (enableLog) {
logLevel = {
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)
Fix Node.js v25 logging prefix and modernize logger (#4049) On Node.js v25, the log prefix in the terminal stopped working - instead of seeing something like: ``` [2026-03-05 23:00:00.000] [LOG] [app] Starting MagicMirror: v2.35.0 ``` the output was: ``` [2026-03-05 23:00:00.000] :pre() Starting MagicMirror: v2.35.0 ``` Reported in #4048. ## Why did it break? The logger used the `console-stamp` package to format log output. One part of that formatting used `styleText("grey", ...)` to color the caller prefix gray. Node.js v25 dropped `"grey"` as a valid color name (only `"gray"` with an "a" is accepted now). This caused `styleText` to throw an error internally - and `console-stamp` silently swallowed that error and fell back to returning its raw `:pre()` format string as the prefix. Not ideal. ## What's in this PR? **1. The actual fix** - `"grey"` → `"gray"`. **2. Cleaner stack trace approach** - the previous code set `Error.prepareStackTrace` *after* creating the `Error`, which is fragile and was starting to behave differently across Node versions. Replaced with straightforward string parsing of `new Error().stack`. **3. Removed the `console-stamp` dependency** - all formatting is now done with plain Node.js built-ins (`node:util` `styleText`). Same visual result, no external dependency. **4. Simplified the module wrapper** - the logger was wrapped in a UMD pattern, which is meant for environments like AMD/RequireJS. MagicMirror only runs in two places: Node.js and the browser. Replaced with a simple check (`typeof module !== "undefined"`), which is much easier to follow.
2026-03-06 13:10:59 +01:00
};
// Only these methods are affected by setLogLevel.
// Utility methods (group, time, etc.) are always active.
Fix Node.js v25 logging prefix and modernize logger (#4049) On Node.js v25, the log prefix in the terminal stopped working - instead of seeing something like: ``` [2026-03-05 23:00:00.000] [LOG] [app] Starting MagicMirror: v2.35.0 ``` the output was: ``` [2026-03-05 23:00:00.000] :pre() Starting MagicMirror: v2.35.0 ``` Reported in #4048. ## Why did it break? The logger used the `console-stamp` package to format log output. One part of that formatting used `styleText("grey", ...)` to color the caller prefix gray. Node.js v25 dropped `"grey"` as a valid color name (only `"gray"` with an "a" is accepted now). This caused `styleText` to throw an error internally - and `console-stamp` silently swallowed that error and fell back to returning its raw `:pre()` format string as the prefix. Not ideal. ## What's in this PR? **1. The actual fix** - `"grey"` → `"gray"`. **2. Cleaner stack trace approach** - the previous code set `Error.prepareStackTrace` *after* creating the `Error`, which is fragile and was starting to behave differently across Node versions. Replaced with straightforward string parsing of `new Error().stack`. **3. Removed the `console-stamp` dependency** - all formatting is now done with plain Node.js built-ins (`node:util` `styleText`). Same visual result, no external dependency. **4. Simplified the module wrapper** - the logger was wrapped in a UMD pattern, which is meant for environments like AMD/RequireJS. MagicMirror only runs in two places: Node.js and the browser. Replaced with a simple check (`typeof module !== "undefined"`), which is much easier to follow.
2026-03-06 13:10:59 +01:00
logLevel.setLogLevel = function (newLevel) {
for (const key of ["debug", "log", "info", "warn", "error"]) {
const disabled = newLevel && !newLevel.includes(key.toUpperCase());
logLevel[key] = disabled ? function () {} : console[key].bind(console);
Fix Node.js v25 logging prefix and modernize logger (#4049) On Node.js v25, the log prefix in the terminal stopped working - instead of seeing something like: ``` [2026-03-05 23:00:00.000] [LOG] [app] Starting MagicMirror: v2.35.0 ``` the output was: ``` [2026-03-05 23:00:00.000] :pre() Starting MagicMirror: v2.35.0 ``` Reported in #4048. ## Why did it break? The logger used the `console-stamp` package to format log output. One part of that formatting used `styleText("grey", ...)` to color the caller prefix gray. Node.js v25 dropped `"grey"` as a valid color name (only `"gray"` with an "a" is accepted now). This caused `styleText` to throw an error internally - and `console-stamp` silently swallowed that error and fell back to returning its raw `:pre()` format string as the prefix. Not ideal. ## What's in this PR? **1. The actual fix** - `"grey"` → `"gray"`. **2. Cleaner stack trace approach** - the previous code set `Error.prepareStackTrace` *after* creating the `Error`, which is fragile and was starting to behave differently across Node versions. Replaced with straightforward string parsing of `new Error().stack`. **3. Removed the `console-stamp` dependency** - all formatting is now done with plain Node.js built-ins (`node:util` `styleText`). Same visual result, no external dependency. **4. Simplified the module wrapper** - the logger was wrapped in a UMD pattern, which is meant for environments like AMD/RequireJS. MagicMirror only runs in two places: Node.js and the browser. Replaced with a simple check (`typeof module !== "undefined"`), which is much easier to follow.
2026-03-06 13:10:59 +01:00
}
};
} else {
logLevel = {
debug () {},
log () {},
info () {},
warn () {},
error () {},
group () {},
groupCollapsed () {},
groupEnd () {},
time () {},
timeEnd () {},
timeStamp () {}
};
Fix Node.js v25 logging prefix and modernize logger (#4049) On Node.js v25, the log prefix in the terminal stopped working - instead of seeing something like: ``` [2026-03-05 23:00:00.000] [LOG] [app] Starting MagicMirror: v2.35.0 ``` the output was: ``` [2026-03-05 23:00:00.000] :pre() Starting MagicMirror: v2.35.0 ``` Reported in #4048. ## Why did it break? The logger used the `console-stamp` package to format log output. One part of that formatting used `styleText("grey", ...)` to color the caller prefix gray. Node.js v25 dropped `"grey"` as a valid color name (only `"gray"` with an "a" is accepted now). This caused `styleText` to throw an error internally - and `console-stamp` silently swallowed that error and fell back to returning its raw `:pre()` format string as the prefix. Not ideal. ## What's in this PR? **1. The actual fix** - `"grey"` → `"gray"`. **2. Cleaner stack trace approach** - the previous code set `Error.prepareStackTrace` *after* creating the `Error`, which is fragile and was starting to behave differently across Node versions. Replaced with straightforward string parsing of `new Error().stack`. **3. Removed the `console-stamp` dependency** - all formatting is now done with plain Node.js built-ins (`node:util` `styleText`). Same visual result, no external dependency. **4. Simplified the module wrapper** - the logger was wrapped in a UMD pattern, which is meant for environments like AMD/RequireJS. MagicMirror only runs in two places: Node.js and the browser. Replaced with a simple check (`typeof module !== "undefined"`), which is much easier to follow.
2026-03-06 13:10:59 +01:00
logLevel.setLogLevel = function () {};
}
return logLevel;
}
}());