簡體   English   中英

模式的 java 監視器日志文件

[英]java monitor log file for a pattern

模式的 Java 監視器日志文件

嗨,大家好,

我需要制作一個程序來監視每行中特定字符串的日志文件(CSV 格式)。 如果字符串出現在日志行中,我想解析日志行並從該行中提取一個字符串。 我想使用這個字符串進行進一步的操作(查找本地 sqlite 數據庫,使用 API 更新其他系統),但我在處理那部分之前已經完成了所有這些操作。

我需要在類似於 tail -f | 的“偵聽”狀態下進行連續監視 grep -i “模式”。

到目前為止,我一直在尋找選項並找到了這一點。 https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/input/Tailer.html

但我不確定如何使用 java.util.regex.* 過濾輸出

我正在尋找最簡單的替代方法來完成這項工作。 任何人都可以提供更好的替代方案或有關如何使用 apache commons Tailer 的一些指導嗎?

以下示例使用 Java 的 WatchService 注冊目錄偵聽器。 每當監視的文件發生更改時,都會讀取該文件,跳過任何先前讀取的內容。 它使用正則表達式來匹配進入的新行,尋找關鍵字。 在這種情況下,“警告|錯誤”。

包com.example;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;

public class LogScanner
{
    Path logFile = Paths.get("app.log");
    private int lines = 0;
    private int characters = 0;

    public static void main(String[] args)
    {
        new LogScanner().run();
    }

    public void run()
    {
        try {
            WatchService watcher = FileSystems.getDefault().newWatchService();

            try (BufferedReader in = new BufferedReader(new FileReader(logFile.toFile()))) {
                String line;
                while ((line = in.readLine()) != null) {
                    lines++;
                    characters += line.length() + System.lineSeparator().length();
                }
            }

            logFile.toAbsolutePath().getParent().register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);

            do {
                WatchKey key = watcher.take();
                System.out.println("Waiting...");
                for (WatchEvent<?> event : key.pollEvents()) {
                    WatchEvent<Path> pathEvent = (WatchEvent<Path>) event;
                    Path path = pathEvent.context();
                    if (path.equals(logFile)) {
                        try (BufferedReader in = new BufferedReader(new FileReader(pathEvent.context().toFile()))) {
                            String line;
                            Pattern p = Pattern.compile("WARN|ERROR");
                            in.skip(characters);
                            while ((line = in.readLine()) != null) {
                                lines++;
                                characters += line.length() + System.lineSeparator().length();
                                if (p.matcher(line).find()) {
                                    // Do something
                                    System.out.println(line);
                                }
                            }
                        }
                    }
                }
                key.reset();
            } while (true);
        } catch (IOException | InterruptedException ex) {
            Logger.getLogger(LogScanner.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM