【问题标题】:java monitor log file for a pattern模式的 java 监视器日志文件
【发布时间】:2017-01-21 00:05:49
【问题描述】:

模式的 Java 监视器日志文件

大家好,

我需要制作一个程序来监视日志文件(CSV 格式)中每行中的特定字符串。如果字符串出现在日志行中,我想解析日志行并从行中提取字符串。我想将此字符串用于进一步的操作(查找本地 sqlite DB,使用 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 file logging text monitor


【解决方案1】:

以下示例使用 Java 的 WatchService 来注册目录侦听器。每当监视的文件发生更改时,就会读取该文件,跳过任何以前读取的内容。它使用正则表达式匹配新行进来,寻找关键字。在本例中为“WARN|ERROR”。

包 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);
        }
    }
}

【讨论】:

  • 太棒了!看起来它会做到的!
  • 如果它对您有用,请您投票并将问题标记为已回答?谢谢!
  • 我正在做一些测试,我第一次运行程序时似乎只有结果,之后似乎没有更多结果。是否与文件已对数旋转有关?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-16
  • 1970-01-01
  • 2010-12-11
  • 1970-01-01
  • 2011-05-19
相关资源
最近更新 更多