Explorar o código

TailReader 中解析失败时写到错误日志文件中

reghao hai 2 meses
pai
achega
1cad5f1ae9

+ 12 - 0
logstash/src/main/java/cn/reghao/bnt/logstash/service/TailReader.java

@@ -1,12 +1,15 @@
 package cn.reghao.bnt.logstash.service;
 
 import cn.reghao.bnt.logstash.ws.WsClient;
+import cn.reghao.jutil.jdk.io.TextFile;
 import cn.reghao.jutil.jdk.serializer.JsonConverter;
 import cn.reghao.jutil.jdk.web.log.NginxLog;
 import lombok.extern.slf4j.Slf4j;
 
 import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.io.RandomAccessFile;
+import java.util.List;
 
 /**
  * @author reghao
@@ -18,12 +21,16 @@ public class TailReader implements Runnable {
     private final RandomAccessFile raf;
     private long pointer;
     private final WsClient wsClient;
+    private TextFile textFile;
+    private String errorLogPath;
 
     public TailReader(String filePath, WsClient wsClient) throws FileNotFoundException {
         this.filePath = filePath;
         this.raf  = new RandomAccessFile(filePath, "r");
         this.pointer = 0;
         this.wsClient = wsClient;
+        this.textFile = new TextFile();
+        this.errorLogPath = System.getProperty("user.dir") + "/logs/error.log";
     }
 
     @Override
@@ -71,6 +78,11 @@ public class TailReader implements Runnable {
             wsClient.send("", nginxLog);
         } catch (Exception e) {
             e.printStackTrace();
+            try {
+                textFile.append(errorLogPath, List.of(line));
+            } catch (IOException ex) {
+                ex.printStackTrace();
+            }
         }
     }
 }