Browse Source

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

hl 5 months ago
parent
commit
d26f901f2d
2 changed files with 6 additions and 6 deletions
  1. 4 4
      src/utils/file.js
  2. 2 2
      src/web.js

+ 4 - 4
src/utils/file.js

@@ -159,7 +159,7 @@ async function readLastNLines(dirPath, filePathList, n) {
     }
 }
 
-async function getRecentLogs(dirPath,logFiles, requiredLogs = 100) {
+async function getRecentLogs(dirPath, logFiles, requiredLogs = 100) {
     let logs = [];
 
     for (const file of logFiles) {
@@ -177,11 +177,11 @@ async function getRecentLogs(dirPath,logFiles, requiredLogs = 100) {
             // logs.push(line);
             log.push(line);
             // 如果已读取到必要的日志条数,则停止
-            if (logs.length >= requiredLogs) {
-                return logs.slice(0, requiredLogs);
+            if ((logs.length + log.length) >= requiredLogs) {
+                // return logs.slice(0, requiredLogs);
+                break
             }
         }
-
         logs.push(...log.reverse());
     }
 

+ 2 - 2
src/web.js

@@ -93,8 +93,8 @@ function init() {
 
                 file.getLastFile(logPath, 5, async (fileNameList, _) => {
                     // logger.info('cccccccccccccc文件:', fileNameList);
-                    result = await file.readLastNLines(logPath, fileNameList, n);
-                    // result =  await  file.getRecentLogs(logPath,fileNameList.reverse(),n);
+                    // 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"});