import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LogErrorScanner {
public static List<String> scanLogFile(String filePath) {
List<String> errors = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
Pattern errorPattern = Pattern.compile("ERROR|Exception|Failed"); //Basic error keyword regex
while ((line = reader.readLine()) != null) {
Matcher matcher = errorPattern.matcher(line);
if (matcher.find()) {
errors.add(line); // Add the line to the list
}
}
} catch (IOException e) {
System.err.println("Error reading file: " + e.getMessage());
}
return errors;
}
public static void main(String[] args) {
// Example usage:
String logFilePath = "sample.log"; // Replace with your log file path
List<String> errorLines = scanLogFile(logFilePath);
if (errorLines.isEmpty()) {
System.out.println("No errors found in the log file.");
} else {
System.out.println("Errors found in the log file:");
for (String error : errorLines) {
System.out.println(error);
}
}
}
}
Add your comment