logger: add calling filename as prefix on server side (#3926)

This commit is contained in:
Karsten Hassel
2025-10-22 22:50:31 +02:00
committed by GitHub
parent a1c1e9560c
commit bc0d36503a
15 changed files with 119 additions and 97 deletions

View File

@@ -6,8 +6,30 @@
// add timestamps in front of log messages
require("console-stamp")(console, {
format: ":date(yyyy-mm-dd HH:MM:ss.l) :label(7) :msg",
format: ":date(yyyy-mm-dd HH:MM:ss.l) :label(7) :pre() :msg",
tokens: {
pre: () => {
const err = new Error();
Error.prepareStackTrace = (_, stack) => stack;
const stack = err.stack;
Error.prepareStackTrace = undefined;
try {
for (const line of stack) {
const file = line.getFileName();
if (file && !file.includes("node:") && !file.includes("js/logger.js") && !file.includes("node_modules")) {
const filename = file.replace(/.*\/(.*).js/, "$1");
const filepath = file.replace(/.*\/(.*)\/.*.js/, "$1");
if (filepath === "js") {
return styleText("grey", `[${filename}]`);
} else {
return styleText("grey", `[${filepath}]`);
}
}
}
} catch (err) {
return styleText("grey", "[unknown]");
}
},
label: (arg) => {
const { method, defaultTokens } = arg;
let label = defaultTokens.label(arg);