Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Google Guava解析日志_Java_Parsing_Logging_Guava - Fatal编程技术网

Java 使用Google Guava解析日志

Java 使用Google Guava解析日志,java,parsing,logging,guava,Java,Parsing,Logging,Guava,我正在研究一种解析日志文件的方法,该文件包含以下格式的日志--(使用Google Guava): Sep 19 2006 13:23:40 MyDevice [latency][info] xmlfirewall (loopback-fw): tid(2809): Latency: 0 1 0 1 1 0 0 1 **999** 1 1 1 0 0 1 1 [http://<IP address>:9999/foo/test.xml] Sep 19 2006 13:23:40 My

我正在研究一种解析日志文件的方法,该文件包含以下格式的日志--(使用Google Guava):

Sep 19 2006 13:23:40 MyDevice [latency][info] xmlfirewall (loopback-fw): tid(2809): Latency: 0 1 0 1 1 0 0 1 **999** 1 1 1 0 0 1 1 [http://<IP address>:9999/foo/test.xml]
Sep 19 2006 13:23:40 MyDevice[latency][info]xmlfirewall(loopback fw):tid(2809):latency:0110101**999**1110101[http://:9999/foo/test.xml]
我正在用谷歌番石榴阅读日志文件

List < String > lines = Files.readLines(new File("C://my.log"), Charsets.UTF_8);
Listlines=Files.readLines(新文件(“C://my.log”)、Charsets.UTF_8);
我想做的是基于用户输入(开始时间、结束时间、IPAddress),我只想拾取那些在开始/结束时间之间有iPadess的行,然后生成如下输出

时间、设备名称、延迟值——在上述情况下,输出将为

05:13:40,我的设备,999


我该怎么做呢。

我认为番石榴在这方面对你没有帮助,我个人也不会把文件读成一大堆行

相反,我会使用正则表达式并在整个文本上运行它,如下所示:

// define pattern as constant
private static final Pattern PATTERN =
Pattern.compile("^.*(?:\\d{1,3}\\.){3}\\d{1,3}.*$",Pattern.MULTILINE);

//now use the pattern in your code (inside a method):
List<String> matchingLines = Lists.newArrayList();
Matcher matcher = PATTERN.matcher(logFileContentsAsString);
while(matcher.find()){
    String line = matcher.group();
    if(performSomeAdditionalTests(line, userData))
        matchingLines.add(line);
}
//将模式定义为常量
私有静态最终模式=
Pattern.compile(“^.*(:\\d{1,3}\\){3}\\d{1,3}.*$”,Pattern.MULTILINE);
//现在在代码中使用模式(在方法中):
List matchingLines=Lists.newArrayList();
Matcher Matcher=PATTERN.Matcher(logFileContentsAsString);
while(matcher.find()){
String line=matcher.group();
if(执行一些附加测试(行、用户数据))
匹配行。添加(行);
}

看看这个方法和界面——我用它对大文件进行流式解析,效果很好。

我对模式/正则表达式不太好,你能分享完整的代码吗