feat(utils): implement structured (JSON) logging

Includes a slight refactor in the console logging section, to achieve an unformatted output and reduce complexity.

Resolves: #5107
This commit is contained in:
Cassandra Beelen 2024-09-19 09:56:47 +02:00
parent 800d868d3f
commit 68b02f1b39
No known key found for this signature in database
2 changed files with 31 additions and 31 deletions

View file

@ -168,7 +168,6 @@ class Logger {
if (this.hideLog[level] && this.hideLog[level].includes(module.toLowerCase())) {
return;
}
module = module.toUpperCase();
let now;
if (dayjs_1.default.tz) {
now = dayjs_1.default.tz(new Date()).format();
@ -178,10 +177,20 @@ class Logger {
}
const levelColor = consoleLevelColors[level];
const moduleColor = consoleModuleColors[intHash(module, consoleModuleColors.length)];
let timePart;
let modulePart;
let levelPart;
let msgPart;
let timePart = now;
let modulePart = module;
let levelPart = level;
let msgPart = msg;
if (process.env.UPTIME_KUMA_LOG_FORMAT === "json") {
console.log(JSON.stringify({
time: timePart,
module: modulePart,
level: levelPart,
msg: typeof msg === "string" ? msg : JSON.stringify(msg),
}));
return;
}
module = module.toUpperCase();
if (exports.isNode) {
switch (level) {
case "DEBUG":
@ -198,28 +207,17 @@ class Logger {
if (typeof msg === "string") {
msgPart = exports.CONSOLE_STYLE_FgRed + msg + exports.CONSOLE_STYLE_Reset;
}
else {
msgPart = msg;
}
break;
case "DEBUG":
if (typeof msg === "string") {
msgPart = exports.CONSOLE_STYLE_FgGray + msg + exports.CONSOLE_STYLE_Reset;
}
else {
msgPart = msg;
}
break;
default:
msgPart = msg;
break;
}
}
else {
timePart = now;
modulePart = `[${module}]`;
levelPart = `${level}:`;
msgPart = msg;
}
switch (level) {
case "ERROR":

View file

@ -216,8 +216,6 @@ class Logger {
return;
}
module = module.toUpperCase();
let now;
if (dayjs.tz) {
now = dayjs.tz(new Date()).format();
@ -228,10 +226,23 @@ class Logger {
const levelColor = consoleLevelColors[level];
const moduleColor = consoleModuleColors[intHash(module, consoleModuleColors.length)];
let timePart: string;
let modulePart: string;
let levelPart: string;
let msgPart: string;
let timePart: string = now;
let modulePart: string = module;
let levelPart: string = level;
let msgPart: unknown = msg;
if (process.env.UPTIME_KUMA_LOG_FORMAT === "json") {
console.log(JSON.stringify({
time: timePart,
module: modulePart,
level: levelPart,
msg: typeof msg === "string" ? msg : JSON.stringify(msg),
}));
return;
}
// Console rendering:
module = module.toUpperCase();
if (isNode) {
// Add console colors
@ -252,27 +263,18 @@ class Logger {
case "ERROR":
if (typeof msg === "string") {
msgPart = CONSOLE_STYLE_FgRed + msg + CONSOLE_STYLE_Reset;
} else {
msgPart = msg;
}
break;
case "DEBUG":
if (typeof msg === "string") {
msgPart = CONSOLE_STYLE_FgGray + msg + CONSOLE_STYLE_Reset;
} else {
msgPart = msg;
}
break;
default:
msgPart = msg;
break;
}
} else {
// No console colors
timePart = now;
modulePart = `[${module}]`;
levelPart = `${level}:`;
msgPart = msg;
}
// Write to console