CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Hybrid View

  1. #1
    Join Date
    Feb 2013
    Posts
    1

    Help: Parse a log file and split every log entry.

    parse a log file and split it by individual log entry. The log entry can be a single line or multiple lines. In addition separate out the timestamp from the rest of the message.

    - Must not load entire file in memory (stream)
    - Must capture the entire entry (single and multiple lines)
    - Must split the entry also in two parts, timestamp and the rest of the entry
    - Must use a regular expression to parse
    - Must be configurable to parse log files in slightly different formats

    example input(part of the log file):

    [18/09/2011 06:53:21:021 PM] ERROR Main 70: Running logger, watch out!
    java.lang.IndexOutOfBoundsException: Index: 99, Size: 0
    at java.util.ArrayList.RangeCheck(ArrayList.java:547)
    at java.util.ArrayList.get(ArrayList.java:322)
    [18/09/2011 06:53:21:021 PM] INFO Main 73: Running logger, watch out!
    [18/09/2011 06:53:21:021 PM] INFO Main 73: Identity manager starting.

    and output 3 log entries:
    1 -
    [18/09/2011 06:53:21:021 PM] ERROR Main 70: Running logger, watch out!
    java.lang.IndexOutOfBoundsException: Index: 99, Size: 0
    at java.util.ArrayList.RangeCheck(ArrayList.java:547)
    at java.util.ArrayList.get(ArrayList.java:322)
    2 -
    [18/09/2011 06:53:21:021 PM] INFO Main 73: Running logger, watch out!
    3 -
    [18/09/2011 06:53:21:021 PM] INFO Main 73: Identity manager starting.


    My questions:

    1. if I use below to read log file, is it correct to meet the requirement (- Must not load entire file in memory (stream))?
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;
    while ((line = br.readLine()) != null) {
    // process the line.
    }
    br.close();

    2. can I save timestamp and the rest of the entry to a HashMap<String, ArrayList<String>>? and key is the timestamp, the rest of entry is saved to ArrayList?
    but if the log file is big, the HashMap should be very big, maybe it is not a good way.

    3. how can I save the result? Can I save the result to a new log file (every entry has a number)?

    Thanks

  2. #2
    Join Date
    Aug 2011
    Location
    West Yorkshire, U.K.
    Posts
    54

    Re: Help: Parse a log file and split every log entry.

    1. That should meet the requirement. You need to be sure that you don't define a buffer or some other temporary storage outside the while loop and write to it inside the loop, otherwise you risk reading the whole file into memory.
    2. If you parse each entry into timestamp + line (where line might be concatenation of a multiple line log entry) and save them in a hash, you are effectively storing the file in memory - i.e. not meeting requirement #1.
    3. You could use a writer to write to an output file, or use System.out to write to standard out.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured