From 68b02f1b3964e290ee762c3d6953b4b3ef3b88ae Mon Sep 17 00:00:00 2001 From: Cassandra Beelen Date: Thu, 19 Sep 2024 09:56:47 +0200 Subject: [PATCH] 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 --- src/util.js | 30 ++++++++++++++---------------- src/util.ts | 32 +++++++++++++++++--------------- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/util.js b/src/util.js index ab8225339..8b2c2b9d7 100644 --- a/src/util.js +++ b/src/util.js @@ -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": diff --git a/src/util.ts b/src/util.ts index 718db6150..4b614a1c8 100644 --- a/src/util.ts +++ b/src/util.ts @@ -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