Optimize: sys log

This commit is contained in:
unitwk 2022-11-17 19:28:01 +08:00
parent 27366bbf31
commit 544b674828

View File

@ -7,16 +7,20 @@ import { systemInfo } from "../common/system_info";
import { $t } from "../i18n";
const LOG_FILE_PATH = "logs/current.log";
const LOG_SYS_INFO_FILE_PATH = "logs/sysinfo.log";
const time = new Date();
const timeString =
`${time.getFullYear()}-${time.getMonth() + 1}-${time.getDate()}` + `_${time.getHours()}-${time.getMinutes()}-${time.getSeconds()}`;
// save the log file separately on each startup
if (fs.existsSync(LOG_FILE_PATH)) {
const time = new Date();
const timeString = `${time.getFullYear()}-${
time.getMonth() + 1
}-${time.getDate()}_${time.getHours()}-${time.getMinutes()}-${time.getSeconds()}`;
fs.renameSync(LOG_FILE_PATH, `logs/${timeString}.log`);
}
if (fs.existsSync(LOG_SYS_INFO_FILE_PATH)) {
fs.renameSync(LOG_SYS_INFO_FILE_PATH, `logs/sysinfo_${timeString}.log`);
}
log4js.configure({
appenders: {
out: {
@ -31,7 +35,15 @@ log4js.configure({
filename: LOG_FILE_PATH,
layout: {
type: "pattern",
pattern: "%d %p %m"
pattern: "[%d{MM/dd hh:mm:ss}] [%p] %m"
}
},
sys: {
type: "file",
filename: LOG_SYS_INFO_FILE_PATH,
layout: {
type: "pattern",
pattern: "[%d{MM/dd hh:mm:ss}] [%p] %m"
}
}
},
@ -39,12 +51,16 @@ log4js.configure({
default: {
appenders: ["out", "app"],
level: "info"
},
sysinfo: {
appenders: ["sys"],
level: "info"
}
}
});
const logger = log4js.getLogger("default");
const loggerSysInfo = log4js.getLogger("sysinfo");
function toInt(v: number) {
return parseInt(String(v));
}
@ -57,12 +73,11 @@ function systemInfoReport() {
const sysInfo =
`MEM: ${toInt((info.totalmem - info.freemem) / MB_SIZE)}MB/${toInt(info.totalmem / MB_SIZE)}MB` + ` CPU: ${toInt(info.cpuUsage * 100)}%`;
const selfInfo = `Heap: ${toInt(self.heapUsed / MB_SIZE)}MB/${toInt(self.heapTotal / MB_SIZE)}MB`;
const selfInfo2 = `RSS: ${toInt(self.rss / MB_SIZE)}MB External: ${toInt(self.external / MB_SIZE)}MB `;
const selfInfo2 = `RSS: ${toInt(self.rss / MB_SIZE)}MB`;
const logTip = $t("app.sysinfo");
logger.info([`[${logTip}]`, sysInfo].join(" "));
logger.info([`[${logTip}]`, selfInfo, selfInfo2].join(" "));
loggerSysInfo.info([`[${logTip}]`, sysInfo, selfInfo, selfInfo2].join(" "));
}
setInterval(systemInfoReport, 1000 * 15);
setInterval(systemInfoReport, 1000 * 5);
export default logger;