mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-02-25 13:05:55 +00:00
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:
parent
800d868d3f
commit
68b02f1b39
2 changed files with 31 additions and 31 deletions
30
src/util.js
30
src/util.js
|
@ -168,7 +168,6 @@ class Logger {
|
||||||
if (this.hideLog[level] && this.hideLog[level].includes(module.toLowerCase())) {
|
if (this.hideLog[level] && this.hideLog[level].includes(module.toLowerCase())) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
module = module.toUpperCase();
|
|
||||||
let now;
|
let now;
|
||||||
if (dayjs_1.default.tz) {
|
if (dayjs_1.default.tz) {
|
||||||
now = dayjs_1.default.tz(new Date()).format();
|
now = dayjs_1.default.tz(new Date()).format();
|
||||||
|
@ -178,10 +177,20 @@ class Logger {
|
||||||
}
|
}
|
||||||
const levelColor = consoleLevelColors[level];
|
const levelColor = consoleLevelColors[level];
|
||||||
const moduleColor = consoleModuleColors[intHash(module, consoleModuleColors.length)];
|
const moduleColor = consoleModuleColors[intHash(module, consoleModuleColors.length)];
|
||||||
let timePart;
|
let timePart = now;
|
||||||
let modulePart;
|
let modulePart = module;
|
||||||
let levelPart;
|
let levelPart = level;
|
||||||
let msgPart;
|
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) {
|
if (exports.isNode) {
|
||||||
switch (level) {
|
switch (level) {
|
||||||
case "DEBUG":
|
case "DEBUG":
|
||||||
|
@ -198,28 +207,17 @@ class Logger {
|
||||||
if (typeof msg === "string") {
|
if (typeof msg === "string") {
|
||||||
msgPart = exports.CONSOLE_STYLE_FgRed + msg + exports.CONSOLE_STYLE_Reset;
|
msgPart = exports.CONSOLE_STYLE_FgRed + msg + exports.CONSOLE_STYLE_Reset;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
msgPart = msg;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case "DEBUG":
|
case "DEBUG":
|
||||||
if (typeof msg === "string") {
|
if (typeof msg === "string") {
|
||||||
msgPart = exports.CONSOLE_STYLE_FgGray + msg + exports.CONSOLE_STYLE_Reset;
|
msgPart = exports.CONSOLE_STYLE_FgGray + msg + exports.CONSOLE_STYLE_Reset;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
msgPart = msg;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
msgPart = msg;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
timePart = now;
|
|
||||||
modulePart = `[${module}]`;
|
modulePart = `[${module}]`;
|
||||||
levelPart = `${level}:`;
|
levelPart = `${level}:`;
|
||||||
msgPart = msg;
|
|
||||||
}
|
}
|
||||||
switch (level) {
|
switch (level) {
|
||||||
case "ERROR":
|
case "ERROR":
|
||||||
|
|
32
src/util.ts
32
src/util.ts
|
@ -216,8 +216,6 @@ class Logger {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
module = module.toUpperCase();
|
|
||||||
|
|
||||||
let now;
|
let now;
|
||||||
if (dayjs.tz) {
|
if (dayjs.tz) {
|
||||||
now = dayjs.tz(new Date()).format();
|
now = dayjs.tz(new Date()).format();
|
||||||
|
@ -228,10 +226,23 @@ class Logger {
|
||||||
const levelColor = consoleLevelColors[level];
|
const levelColor = consoleLevelColors[level];
|
||||||
const moduleColor = consoleModuleColors[intHash(module, consoleModuleColors.length)];
|
const moduleColor = consoleModuleColors[intHash(module, consoleModuleColors.length)];
|
||||||
|
|
||||||
let timePart: string;
|
let timePart: string = now;
|
||||||
let modulePart: string;
|
let modulePart: string = module;
|
||||||
let levelPart: string;
|
let levelPart: string = level;
|
||||||
let msgPart: string;
|
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) {
|
if (isNode) {
|
||||||
// Add console colors
|
// Add console colors
|
||||||
|
@ -252,27 +263,18 @@ class Logger {
|
||||||
case "ERROR":
|
case "ERROR":
|
||||||
if (typeof msg === "string") {
|
if (typeof msg === "string") {
|
||||||
msgPart = CONSOLE_STYLE_FgRed + msg + CONSOLE_STYLE_Reset;
|
msgPart = CONSOLE_STYLE_FgRed + msg + CONSOLE_STYLE_Reset;
|
||||||
} else {
|
|
||||||
msgPart = msg;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "DEBUG":
|
case "DEBUG":
|
||||||
if (typeof msg === "string") {
|
if (typeof msg === "string") {
|
||||||
msgPart = CONSOLE_STYLE_FgGray + msg + CONSOLE_STYLE_Reset;
|
msgPart = CONSOLE_STYLE_FgGray + msg + CONSOLE_STYLE_Reset;
|
||||||
} else {
|
|
||||||
msgPart = msg;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
|
||||||
msgPart = msg;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// No console colors
|
// No console colors
|
||||||
timePart = now;
|
|
||||||
modulePart = `[${module}]`;
|
modulePart = `[${module}]`;
|
||||||
levelPart = `${level}:`;
|
levelPart = `${level}:`;
|
||||||
msgPart = msg;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write to console
|
// Write to console
|
||||||
|
|
Loading…
Add table
Reference in a new issue