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
- Check if line contains level
- Print matching lines only
- Consider using indexOf or contains
Related File I/O exercises
- Practice Simple CSV Reader in Java
- Practice Find and Replace in File in Java
- Practice Word Frequency Counter in Java
Practice all File I/O exercises · Run this idea in the Java compiler