refactor(logging): improve log function parameters (#6298)
This commit is contained in:
parent
9fb4263427
commit
a52186cf7e
@ -59,7 +59,7 @@ if (process.env.UPTIME_KUMA_WS_ORIGIN_CHECK === "bypass") {
|
||||
}
|
||||
|
||||
const checkVersion = require("./check-version");
|
||||
log.info("server", "Uptime Kuma Version: " + checkVersion.version);
|
||||
log.info("server", "Uptime Kuma Version:", checkVersion.version);
|
||||
|
||||
log.info("server", "Loading modules");
|
||||
|
||||
|
||||
57
src/util.js
57
src/util.js
@ -133,7 +133,7 @@ function ucfirst(str) {
|
||||
}
|
||||
exports.ucfirst = ucfirst;
|
||||
function debug(msg) {
|
||||
exports.log.log("", msg, "debug");
|
||||
exports.log.log("", "debug", msg);
|
||||
}
|
||||
exports.debug = debug;
|
||||
class Logger {
|
||||
@ -156,7 +156,7 @@ class Logger {
|
||||
this.debug("server", this.hideLog);
|
||||
}
|
||||
}
|
||||
log(module, msg, level) {
|
||||
log(module, level, ...msg) {
|
||||
if (level === "DEBUG" && !exports.isDev) {
|
||||
return;
|
||||
}
|
||||
@ -177,7 +177,6 @@ class Logger {
|
||||
let timePart;
|
||||
let modulePart;
|
||||
let levelPart;
|
||||
let msgPart;
|
||||
if (exports.isNode) {
|
||||
switch (level) {
|
||||
case "DEBUG":
|
||||
@ -189,72 +188,50 @@ class Logger {
|
||||
}
|
||||
modulePart = "[" + moduleColor + module + exports.CONSOLE_STYLE_Reset + "]";
|
||||
levelPart = levelColor + `${level}:` + exports.CONSOLE_STYLE_Reset;
|
||||
switch (level) {
|
||||
case "ERROR":
|
||||
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":
|
||||
console.error(timePart, modulePart, levelPart, msgPart);
|
||||
console.error(timePart, modulePart, levelPart, ...msg);
|
||||
break;
|
||||
case "WARN":
|
||||
console.warn(timePart, modulePart, levelPart, msgPart);
|
||||
console.warn(timePart, modulePart, levelPart, ...msg);
|
||||
break;
|
||||
case "INFO":
|
||||
console.info(timePart, modulePart, levelPart, msgPart);
|
||||
console.info(timePart, modulePart, levelPart, ...msg);
|
||||
break;
|
||||
case "DEBUG":
|
||||
if (exports.isDev) {
|
||||
console.debug(timePart, modulePart, levelPart, msgPart);
|
||||
console.debug(timePart, modulePart, levelPart, ...msg);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
console.log(timePart, modulePart, levelPart, msgPart);
|
||||
console.log(timePart, modulePart, levelPart, ...msg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
info(module, msg) {
|
||||
this.log(module, msg, "info");
|
||||
info(module, ...msg) {
|
||||
this.log(module, "info", ...msg);
|
||||
}
|
||||
warn(module, msg) {
|
||||
this.log(module, msg, "warn");
|
||||
warn(module, ...msg) {
|
||||
this.log(module, "warn", ...msg);
|
||||
}
|
||||
error(module, msg) {
|
||||
this.log(module, msg, "error");
|
||||
error(module, ...msg) {
|
||||
this.log(module, "error", ...msg);
|
||||
}
|
||||
debug(module, msg) {
|
||||
this.log(module, msg, "debug");
|
||||
debug(module, ...msg) {
|
||||
this.log(module, "debug", ...msg);
|
||||
}
|
||||
exception(module, exception, msg) {
|
||||
exception(module, exception, ...msg) {
|
||||
let finalMessage = exception;
|
||||
if (msg) {
|
||||
finalMessage = `${msg}: ${exception}`;
|
||||
}
|
||||
this.log(module, finalMessage, "error");
|
||||
this.log(module, "error", finalMessage);
|
||||
}
|
||||
}
|
||||
exports.log = new Logger();
|
||||
|
||||
64
src/util.ts
64
src/util.ts
@ -193,7 +193,7 @@ export function ucfirst(str: string) {
|
||||
* @returns {void}
|
||||
*/
|
||||
export function debug(msg: unknown) {
|
||||
log.log("", msg, "debug");
|
||||
log.log("", "debug", msg);
|
||||
}
|
||||
|
||||
class Logger {
|
||||
@ -238,11 +238,11 @@ class Logger {
|
||||
/**
|
||||
* Write a message to the log
|
||||
* @param module The module the log comes from
|
||||
* @param msg Message to write
|
||||
* @param level Log level. One of INFO, WARN, ERROR, DEBUG or can be customized.
|
||||
* @param msg Message to write
|
||||
* @returns {void}
|
||||
*/
|
||||
log(module: string, msg: any, level: string) {
|
||||
log(module: string, level: string, ...msg: unknown[]) {
|
||||
if (level === "DEBUG" && !isDev) {
|
||||
return;
|
||||
}
|
||||
@ -267,7 +267,6 @@ class Logger {
|
||||
let timePart: string;
|
||||
let modulePart: string;
|
||||
let levelPart: string;
|
||||
let msgPart: string;
|
||||
|
||||
if (isNode) {
|
||||
// Add console colors
|
||||
@ -281,54 +280,33 @@ class Logger {
|
||||
}
|
||||
|
||||
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;
|
||||
} 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
|
||||
switch (level) {
|
||||
case "ERROR":
|
||||
console.error(timePart, modulePart, levelPart, msgPart);
|
||||
console.error(timePart, modulePart, levelPart, ...msg);
|
||||
break;
|
||||
case "WARN":
|
||||
console.warn(timePart, modulePart, levelPart, msgPart);
|
||||
console.warn(timePart, modulePart, levelPart, ...msg);
|
||||
break;
|
||||
case "INFO":
|
||||
console.info(timePart, modulePart, levelPart, msgPart);
|
||||
console.info(timePart, modulePart, levelPart, ...msg);
|
||||
break;
|
||||
case "DEBUG":
|
||||
if (isDev) {
|
||||
console.debug(timePart, modulePart, levelPart, msgPart);
|
||||
console.debug(timePart, modulePart, levelPart, ...msg);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
console.log(timePart, modulePart, levelPart, msgPart);
|
||||
console.log(timePart, modulePart, levelPart, ...msg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -339,8 +317,8 @@ class Logger {
|
||||
* @param msg Message to write
|
||||
* @returns {void}
|
||||
*/
|
||||
info(module: string, msg: unknown) {
|
||||
this.log(module, msg, "info");
|
||||
info(module: string, ...msg: unknown[]) {
|
||||
this.log(module, "info", ...msg);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -349,8 +327,8 @@ class Logger {
|
||||
* @param msg Message to write
|
||||
* @returns {void}
|
||||
*/
|
||||
warn(module: string, msg: unknown) {
|
||||
this.log(module, msg, "warn");
|
||||
warn(module: string, ...msg: unknown[]) {
|
||||
this.log(module, "warn", ...msg);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -359,8 +337,8 @@ class Logger {
|
||||
* @param msg Message to write
|
||||
* @returns {void}
|
||||
*/
|
||||
error(module: string, msg: unknown) {
|
||||
this.log(module, msg, "error");
|
||||
error(module: string, ...msg: unknown[]) {
|
||||
this.log(module, "error", ...msg);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -369,8 +347,8 @@ class Logger {
|
||||
* @param msg Message to write
|
||||
* @returns {void}
|
||||
*/
|
||||
debug(module: string, msg: unknown) {
|
||||
this.log(module, msg, "debug");
|
||||
debug(module: string, ...msg: unknown[]) {
|
||||
this.log(module, "debug", ...msg);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -380,14 +358,8 @@ class Logger {
|
||||
* @param msg The message to write
|
||||
* @returns {void}
|
||||
*/
|
||||
exception(module: string, exception: unknown, msg: unknown) {
|
||||
let finalMessage = exception;
|
||||
|
||||
if (msg) {
|
||||
finalMessage = `${msg}: ${exception}`;
|
||||
}
|
||||
|
||||
this.log(module, finalMessage, "error");
|
||||
exception(module: string, exception: unknown, ...msg: unknown[]) {
|
||||
this.log(module, "error", ...msg, exception);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user