user454894
user454894

Reputation: 31

Parsing a log file in Java

All,

I have a log file with the below content.

Request from Centercord.
2010-12-14 12:42:13.724 [ 6796] ****************************
2010-12-14 12:42:13.724 [ 6796] 1111111111111111
2010-12-14 12:42:13.724 [ 6796]22222222222

Response from Centercord.
2010-12-14 12:42:21.802 [ 5960] 11111111111111
2010-12-14 12:42:21.802 [ 5960]  ffffffffffffffffffffffffffff
2010-12-14 12:42:21.802 [ 5960]  tttttttttttttttttttttttttttt

Request from Centercord.
2010-12-14 12:42:13.724 [ 6796] ****************************

I need to create two log files one is for storing all the Request details and the other one is for storing all the Response details. How can I parse this and prepare two log files?.

I need the below answer.

Log 1:

Request from Centercord.
2010-12-14 12:42:13.724 [ 6796] ****************************
2010-12-14 12:42:13.724 [ 6796] 1111111111111111
2010-12-14 12:42:13.724 [ 6796]22222222222

2010-12-14 12:42:13.724 [ 6796] ****************************

Log 2:

Response from Centercord.
2010-12-14 12:42:21.802 [ 5960] 11111111111111
2010-12-14 12:42:21.802 [ 5960]  ffffffffffffffffffffffffffff
2010-12-14 12:42:21.802 [ 5960]  tttttttttttttttttttttttttttt

Regards, Kanagaraj

Upvotes: 3

Views: 25422

Answers (4)

darioo
darioo

Reputation: 47213

For reducing boilerplate code, download Google Guava and use this code:

File inputFile = new File("input.log");
File outputLog1 = new File("log1.log");
File outputLog2 = new File("log2.log");

String match1 = "[ 6796]";
String match2 = "[ 5960]";

Files.append("Request from Centercord.\n", outputLog1, Charsets.UTF_8);
Files.append("Response from Centercord.\n", outputLog2, Charsets.UTF_8);

List<String> lines = Files.readLines(inputFile, Charsets.UTF_8);

for (String line : lines) {
    if(line.contains(match1)) Files.append(line + "\n", outputLog1, Charsets.UTF_8);
    if(line.contains(match2)) Files.append(line + "\n", outputLog2, Charsets.UTF_8);
}

Upvotes: 0

aioobe
aioobe

Reputation: 421340

Here is how I would do it:

import java.io.*;
import java.util.Scanner;

public class Test {

    public static void main(String[] args) {

        try {

            PrintWriter requests = new PrintWriter("requests.txt");
            PrintWriter responses = new PrintWriter("responses.txt");
            PrintWriter currentLog = null;

            Scanner s = new Scanner(new File("log.txt"));
            while (s.hasNextLine()) {
                String line = s.nextLine();
                if (line.startsWith("Request from"))
                    currentLog = requests;
                else if (line.startsWith("Response from"))
                    currentLog = responses;
                else if (currentLog != null)
                    currentLog.println(line);
            }

            requests.close();
            responses.close();
            s.close();
        } catch (IOException ioex) {
            // handle exception...
        }
    }
}

Given log.txt

Request from Centercord.
2010-12-14 12:42:13.724 [ 6796] ****************************
2010-12-14 12:42:13.724 [ 6796] 1111111111111111
2010-12-14 12:42:13.724 [ 6796]22222222222

Response from Centercord.
2010-12-14 12:42:21.802 [ 5960] 11111111111111
2010-12-14 12:42:21.802 [ 5960]  ffffffffffffffffffffffffffff
2010-12-14 12:42:21.802 [ 5960]  tttttttttttttttttttttttttttt

Request from Centercord.
2010-12-14 12:42:13.724 [ 6796] ****************************

It produces, requests.txt

2010-12-14 12:42:13.724 [ 6796] ****************************
2010-12-14 12:42:13.724 [ 6796] 1111111111111111
2010-12-14 12:42:13.724 [ 6796]22222222222

2010-12-14 12:42:13.724 [ 6796] ****************************

...and responses.txt

2010-12-14 12:42:21.802 [ 5960] 11111111111111
2010-12-14 12:42:21.802 [ 5960]  ffffffffffffffffffffffffffff
2010-12-14 12:42:21.802 [ 5960]  tttttttttttttttttttttttttttt

Upvotes: 10

Andreas Dolk
Andreas Dolk

Reputation: 114847

StringBuilder requestBuilder = new StringBuilder("Request from Centercord.\n");
StringBuilder responseBuilder = new StringBuilder("Response from Centercord.\n");

boolean isResponse = false;
for (String line:getLinesFromLogFile()) {
  if (line.startsWith("Response")) {
      if(isResponse)
        responseBuilder.append("\n");
      isResponse = true;
  } else if (line.startsWith("Request")) {
      if(!isResponse)
        requestBuilder.append("\n");
      isResponse = false;
  } else {
      if (isResponse) {
         responseBuilder.append(line).append("\n");
      } else {
         requestBuilder.append(line).append("\n");
      }
  }
}

Now dump the contents of both StringBuilders to the target files.

Upvotes: 0

iuiz
iuiz

Reputation: 965

You can use regular expressions or RandomAccessFiles to get the data in the way you want.

Upvotes: 0

Related Questions