Просмотр исходного кода

优化日志获取-发现长时间获取会触发100CPU,本次优化尝试解决该问题

hl 5 месяцев назад
Родитель
Сommit
019237de94
2 измененных файлов с 36 добавлено и 12 удалено
  1. 32 10
      src/utils/file.js
  2. 4 2
      src/web.js

+ 32 - 10
src/utils/file.js

@@ -106,13 +106,13 @@ function getLastFile(dirPath, number, callback) {
                 const stats = await fs.promises.stat(filePath);
                 if (stats.isFile()) {
                     fileList.push({name: file, time: stats.birthtime.getTime()});
-                    // logger.info("文件:" + filePath + "时间:" + stats.birthtime.getTime())
+                    // logger.info("----------------文件:" + filePath + "时间:" + stats.birthtime.getTime())
                 }
             }
 
             fileList.sort((a, b) => b.time - a.time);
             // for (a of fileList) {
-            //     logger.info("文件cccccccc22222222:" + a.name + "\ttime:" + a.time)
+            //     logger.info("cccccccccc文件cccccccc22222222:" + a.name + "\ttime:" + a.time)
             // }
             lastFileList = fileList.slice(0, number != -1 ? number : fileList.length);
             lastFileNameList = lastFileList.map((item) => item.name);
@@ -125,11 +125,6 @@ function getLastFile(dirPath, number, callback) {
 // 读取日志(倒叙)指定行数
 async function readLastNLines(dirPath, filePathList, n) {
     try {
-        // for (var i = 0; i < filePathList.length; i++) {
-        //     var item = filePathList[i];
-        //     logger.info("日志文件2222222:" + item)
-        // }
-
         const fileList = [];
         var count = 0;
         for (var i = filePathList.length - 1; i >= 0; i--) {
@@ -155,9 +150,6 @@ async function readLastNLines(dirPath, filePathList, n) {
         }
         fileList.reverse();
 
-        // const allFile = [].concat(...fileList.map((item) => item.lines));
-        // let lastNLines = allFile.slice(-n).join("\n");
-        // return lastNLines.split("\n").reverse();
 
         const allFile = [].concat(...fileList.map((item) => item.lines));
         const lastNLines = allFile.slice(-n).reverse();
@@ -167,6 +159,35 @@ async function readLastNLines(dirPath, filePathList, n) {
     }
 }
 
+async function getRecentLogs(dirPath,logFiles, requiredLogs = 500) {
+    let logs = [];
+
+    for (const file of logFiles) {
+        let log = [];
+        // 创建文件流
+        const filepath = `${dirPath}/${file}`;
+        const fileStream = fs.createReadStream(filepath);
+
+        const rl = readline.createInterface({
+            input: fileStream,
+            crlfDelay: Infinity
+        });
+
+        for await (const line of rl) {
+            // logs.push(line);
+            log.push(line);
+            // 如果已读取到必要的日志条数,则停止
+            if (logs.length >= requiredLogs) {
+                return logs.slice(0, requiredLogs);
+            }
+        }
+
+        logs.push(...log.reverse());
+    }
+
+    return logs; // 如果日志不够500条,返回现有日志
+}
+
 
 async function readLinesFromEnd(filePath, maxLines) {
     const lines = [];
@@ -270,5 +291,6 @@ module.exports = {
     writeFile,
     copyFileSync,
     readLastNLines,
+    getRecentLogs,
     getLastFile
 }

+ 4 - 2
src/web.js

@@ -92,8 +92,10 @@ function init() {
                 file.checkPathSync(directoryPath)
 
                 file.getLastFile(logPath, 500, async (fileNameList, _) => {
-                    // logger.info('文件:', fileNameList);
-                    result = await file.readLastNLines(logPath, fileNameList, n);
+                    // logger.info('cccccccccccccc文件:', fileNameList);
+                    // result = await file.readLastNLines(logPath, fileNameList, n);
+                    result =  await  file.getRecentLogs(logPath,fileNameList.reverse(),n);
+
                     // logger.info('?????:', result);
                     res.send({'code': 200, 'data': result, "message": "SUCCESS"});
                 });