Parsing Logs Using Google Guava
I am looking at way of parsing log file having log -- (Using Google Guava) in below format:
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]
I am reading log file using Google Guava
List < String > lines = Files.readLines(new File("C://my.log"), Charsets.UTF_8);
What I want to do is based on the user input (Start Time, End Time, IPAddress), I want to pickup only those line where we have IPAddess between start/end time and the开发者_运维问答n produce an output like this
Time,DeviceName,LatencyValue -- In the above case the output will be
05:13:40,MyDevice,999
How Should I go about it.
Take a look at the CharStreams.readLines method and the LineProcessor interface -- I've used that to do streaming parses of large files with good results.
I don't think Guava will help you there, and I personally wouldn't read the file to a list of lines, either.
Instead, I'd use a regular expression and run it over the entire text, like so:
// 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);
}
精彩评论