Revert "feat: structured logging (JSON)" (#5175)

This commit is contained in:
Louis Lam
2024-10-09 07:43:44 +08:00
committed by GitHub
parent 4829ad8c5d
commit eca90a2b00
27 changed files with 239 additions and 186 deletions

View File

@@ -156,6 +156,15 @@ export function ucfirst(str: string) {
return firstLetter.toUpperCase() + str.substr(1);
}
/**
* @deprecated Use log.debug (https://github.com/louislam/uptime-kuma/pull/910)
* @param msg Message to write
* @returns {void}
*/
export function debug(msg: unknown) {
log.log("", msg, "debug");
}
class Logger {
/**
@@ -197,13 +206,12 @@ class Logger {
/**
* Write a message to the log
* @private
* @param module The module the log comes from
* @param msg Message to write
* @param level {"INFO"|"WARN"|"ERROR"|"DEBUG"} Log level
* @param level Log level. One of INFO, WARN, ERROR, DEBUG or can be customized.
* @returns {void}
*/
log(module: string, msg: unknown, level: "INFO"|"WARN"|"ERROR"|"DEBUG"): void {
log(module: string, msg: any, level: string) {
if (level === "DEBUG" && !isDev) {
return;
}
@@ -212,6 +220,9 @@ class Logger {
return;
}
module = module.toUpperCase();
level = level.toUpperCase();
let now;
if (dayjs.tz) {
now = dayjs.tz(new Date()).format();
@@ -222,23 +233,10 @@ class Logger {
const levelColor = consoleLevelColors[level];
const moduleColor = consoleModuleColors[intHash(module, consoleModuleColors.length)];
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();
let timePart: string;
let modulePart: string;
let levelPart: string;
let msgPart: string;
if (isNode) {
// Add console colors
@@ -259,18 +257,27 @@ 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
@@ -301,8 +308,8 @@ class Logger {
* @param msg Message to write
* @returns {void}
*/
info(module: string, msg: string): void {
this.log(module, msg, "INFO");
info(module: string, msg: unknown) {
this.log(module, msg, "info");
}
/**
@@ -311,8 +318,8 @@ class Logger {
* @param msg Message to write
* @returns {void}
*/
warn(module: string, msg: string): void {
this.log(module, msg, "WARN");
warn(module: string, msg: unknown) {
this.log(module, msg, "warn");
}
/**
@@ -321,8 +328,8 @@ class Logger {
* @param msg Message to write
* @returns {void}
*/
error(module: string, msg: string): void {
this.log(module, msg, "ERROR");
error(module: string, msg: unknown) {
this.log(module, msg, "error");
}
/**
@@ -331,8 +338,8 @@ class Logger {
* @param msg Message to write
* @returns {void}
*/
debug(module: string, msg: string): void {
this.log(module, msg, "DEBUG");
debug(module: string, msg: unknown) {
this.log(module, msg, "debug");
}
/**
@@ -349,7 +356,7 @@ class Logger {
finalMessage = `${msg}: ${exception}`;
}
this.log(module, finalMessage, "ERROR");
this.log(module, finalMessage, "error");
}
}
@@ -393,7 +400,7 @@ export class TimeLogger {
* @param name Name of monitor
* @returns {void}
*/
print(name: string): void {
print(name: string) {
if (isDev && process.env.TIMELOGGER === "1") {
console.log(name + ": " + (dayjs().valueOf() - this.startTime) + "ms");
}