Skip to content

Fix Node.js v25 logging prefix and modernize logger#4049

Merged
rejas merged 4 commits intoMagicMirrorOrg:developfrom
KristjanESPERANTO:logger
Mar 6, 2026
Merged

Fix Node.js v25 logging prefix and modernize logger#4049
rejas merged 4 commits intoMagicMirrorOrg:developfrom
KristjanESPERANTO:logger

Conversation

@KristjanESPERANTO
Copy link
Collaborator

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.

…n logger

The previous approach set Error.prepareStackTrace after creating the
Error object, which is fragile and relies on lazy evaluation of err.stack.
Replace with direct string parsing of new Error().stack using a regex,
which is simpler and more robust across Node.js versions.
Node.js v25 removed "grey" as a valid color name for styleText().
Only "gray" is accepted. This caused styleText() to throw inside
the pre token callback, which console-stamp silently caught and
returned the raw ":pre()" format string as the prefix.

Fixes MagicMirrorOrg#4048
The UMD (Universal Module Definition) wrapper was unnecessary overhead.
The logger only ever needs to run in two environments: Node.js (CJS) and
the browser (global). Replace with a plain IIFE that checks
typeof module directly.

Also removes the unused 'config' parameter that was passed through
the UMD factory but never referenced inside it.
Remove the console-stamp dependency and replicate its behavior
directly using Node.js built-ins only (node:util styleText).

Also clean up the new implementation:
- Fix regex bug: unescaped dot in .js patterns (. → \.)
- Rename confusing variables: filename/filepath → baseName/parentDir
- Split formatTimestamp into readable date/time variables
- Update outdated top comment
@rejas rejas merged commit 3eb3745 into MagicMirrorOrg:develop Mar 6, 2026
9 checks passed
khassel pushed a commit that referenced this pull request Mar 6, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants