Log File Parser in Java: Explanation & Practice

Parse and filter log entries

Problem summary

Parse a log file and extract entries matching a severity level.

Starter code

import java.io.*;

public class Main {
    public static void main(String[] args) {
        String level = "ERROR"; // Test case 1
        
        // Filter log entries by level
        // Print matching entries
    }
}

Expected output and test cases

  • Error entries
    2024-01-15 10:30:00 ERROR Database connection failed
    2024-01-15 10:35:00 ERROR Timeout occurred
  • Warning entries
    2024-01-15 10:32:00 WARN Low memory
  • No matches

Hints

  1. Check if line contains level
  2. Print matching lines only
  3. Consider using indexOf or contains

Related File I/O exercises

Practice all File I/O exercises · Run this idea in the Java compiler