Skip to content

Commit

Permalink
feat(utils): implement structured (JSON) logging
Browse files Browse the repository at this point in the history
Includes a slight refactor in the console logging section, to achieve an unformatted output and reduce complexity.

Resolves: louislam#5107
  • Loading branch information
SapphicCode committed Sep 19, 2024
1 parent 4eaf9c7 commit 6180775
Showing 1 changed file with 61 additions and 55 deletions.
116 changes: 61 additions & 55 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,84 +212,90 @@ class Logger {
return;
}

module = module.toUpperCase();

let now;
if (dayjs.tz) {
now = dayjs.tz(new Date()).format();
} else {
now = dayjs().format();
}

const structuredLogging = process.env.UPTIME_KUMA_LOG_FORMAT === "json";

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 (!structuredLogging) {
// Console rendering:
module = module.toUpperCase();

if (isNode) {
// Add console colors
switch (level) {
case "DEBUG":
timePart = CONSOLE_STYLE_FgGray + now + CONSOLE_STYLE_Reset;
break;
default:
timePart = CONSOLE_STYLE_FgCyan + now + CONSOLE_STYLE_Reset;
break;
}

if (isNode) {
// Add console colors
switch (level) {
case "DEBUG":
timePart = CONSOLE_STYLE_FgGray + now + CONSOLE_STYLE_Reset;
break;
default:
timePart = CONSOLE_STYLE_FgCyan + now + CONSOLE_STYLE_Reset;
break;
modulePart = "[" + moduleColor + module + CONSOLE_STYLE_Reset + "]";

levelPart = levelColor + `${level}:` + CONSOLE_STYLE_Reset;

switch (level) {
case "ERROR":
if (typeof msg === "string") {
msgPart = CONSOLE_STYLE_FgRed + msg + CONSOLE_STYLE_Reset;
}
break;
case "DEBUG":
if (typeof msg === "string") {
msgPart = CONSOLE_STYLE_FgGray + msg + CONSOLE_STYLE_Reset;
}
break;
default:
break;
}
} else {
// No console colors
timePart = now;
modulePart = `[${module}]`;
levelPart = `${level}:`;
}

modulePart = "[" + moduleColor + module + CONSOLE_STYLE_Reset + "]";

levelPart = levelColor + `${level}:` + CONSOLE_STYLE_Reset;

// Write to console
switch (level) {
case "ERROR":
if (typeof msg === "string") {
msgPart = CONSOLE_STYLE_FgRed + msg + CONSOLE_STYLE_Reset;
} else {
msgPart = String(msg);
}
console.error(timePart, modulePart, levelPart, msgPart);
break;
case "WARN":
console.warn(timePart, modulePart, levelPart, msgPart);
break;
case "INFO":
console.info(timePart, modulePart, levelPart, msgPart);
break;
case "DEBUG":
if (typeof msg === "string") {
msgPart = CONSOLE_STYLE_FgGray + msg + CONSOLE_STYLE_Reset;
} else {
msgPart = String(msg);
if (isDev) {
console.debug(timePart, modulePart, levelPart, msgPart);
}
break;
default:
msgPart = String(msg);
console.log(timePart, modulePart, levelPart, msgPart);
break;
}
} else {
// No console colors
timePart = now;
modulePart = `[${module}]`;
levelPart = `${level}:`;
msgPart = String(msg);
}

// Write to console
switch (level) {
case "ERROR":
console.error(timePart, modulePart, levelPart, msgPart);
break;
case "WARN":
console.warn(timePart, modulePart, levelPart, msgPart);
break;
case "INFO":
console.info(timePart, modulePart, levelPart, msgPart);
break;
case "DEBUG":
if (isDev) {
console.debug(timePart, modulePart, levelPart, msgPart);
}
break;
default:
console.log(timePart, modulePart, levelPart, msgPart);
break;
console.log(JSON.stringify({
time: timePart,
module: modulePart,
level: levelPart,
msg: typeof msg === "string" ? msg : JSON.stringify(msg),
}))
}
}

Expand Down

0 comments on commit 6180775

Please sign in to comment.