Browse Source

优化日志获取

hl 7 months ago
parent
commit
0c7ccdf030
1 changed files with 24 additions and 59 deletions
  1. 24 59
      src/utils/file.js

+ 24 - 59
src/utils/file.js

@@ -142,7 +142,7 @@ async function readLastNLines(dirPath, filePathList, n) {
             // let lines = content.trim().split("\n");
 
             //新版本:流式读取和逐行处理
-            const lines = await readLinesFromEnd(filepath,n-count);
+            const lines = await readLinesFromEnd(filepath, n - count);
             logger.info("日:" + lines)
             fileList.push({
                 filePath: item,
@@ -172,21 +172,19 @@ async function readLinesFromEnd(filePath, maxLines) {
     const lines = [];
     const fileSize = fs.statSync(filePath).size;
     const chunkSize = 1024; // 每次读取的块大小
-    let buffer = '';
 
 
-    let forNum = (fileSize/chunkSize)+1;
-    for  (var i = 1 ;i <= forNum;i++){
-        let position = fileSize - (chunkSize * i);
-        if(position < 0){
-            position = 0;
-        }
 
+    let forNum = (fileSize / chunkSize) + 1;
+    for (let i = 1;i<= forNum; i++) {
+        let position = fileSize - chunkSize *i;
+        let z = position + chunkSize
         const fileStream = fs.createReadStream(filePath, {
             start: Math.max(position, 0),
-            end: fileSize
+            end: z
         });
 
+        let buffer = '';
         for await (const chunk of fileStream) {
             buffer = chunk + buffer;
             let lineEndIndex = buffer.length;
@@ -212,63 +210,30 @@ async function readLinesFromEnd(filePath, maxLines) {
                 position = 0;
             }
         }
+    }
+
+    return lines;
+}
+
+async function readLines(filePath, maxLines) {
+    const fileStream = fs.createReadStream(filePath);
+    const rl = readline.createInterface({
+        input: fileStream,
+        crlfDelay: Infinity
+    });
+
+    const lines = [];
+    for await   (const line of rl) {
+        lines.push(line);
         if (lines.length >= maxLines) {
             break;
         }
     }
 
-    // const fileStream = fs.createReadStream(filePath, {
-    //     start: Math.max(position, 0),
-    //     end: fileSize
-    // });
-
-    // for await (const chunk of fileStream) {
-    //     buffer = chunk + buffer;
-    //     let lineEndIndex = buffer.length;
-    //
-    //     while (lineEndIndex >= 0 && lines.length < maxLines) {
-    //         const lineStartIndex = buffer.lastIndexOf('\n', lineEndIndex - 1);
-    //         if (lineStartIndex === -1) {
-    //             break;
-    //         }
-    //         const line = buffer.substring(lineStartIndex + 1, lineEndIndex).trim();
-    //         if (line) {
-    //             lines.push(line);
-    //         }
-    //         lineEndIndex = lineStartIndex;
-    //     }
-    //
-    //     if (lines.length >= maxLines) {
-    //         break;
-    //     }
-    //
-    //     position -= chunkSize;
-    //     if (position < 0) {
-    //         position = 0;
-    //     }
-    // }
-
+    rl.close();
+    fileStream.close();
     return lines;
 }
- async function readLines(filePath, maxLines) {
-     const fileStream = fs.createReadStream(filePath);
-     const rl = readline.createInterface({
-         input: fileStream,
-         crlfDelay: Infinity
-     });
-
-     const lines = [];
-     for await   (const line of rl) {
-         lines.push(line);
-         if (lines.length >= maxLines) {
-             break;
-         }
-     }
-
-     rl.close();
-     fileStream.close();
-     return lines;
- }
 
 
 module.exports = {