1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. public class LogValidator {
  7. public static void main(String[] args) {
  8. List<String> logFiles = List.of("log1.txt", "log2.txt"); // Replace with your log file names
  9. for (String logFile : logFiles) {
  10. try {
  11. validateLogFile(logFile);
  12. } catch (IOException e) {
  13. System.err.println("Error processing log file " + logFile + ": " + e.getMessage());
  14. } catch (Exception e) {
  15. System.err.println("Unexpected error processing log file " + logFile + ": " + e.getMessage());
  16. }
  17. }
  18. }
  19. public static void validateLogFile(String logFile) throws IOException {
  20. try (BufferedReader reader = new BufferedReader(new FileReader(logFile))) {
  21. String line;
  22. int lineNumber = 1;
  23. boolean validationSuccessful = true;
  24. while ((line = reader.readLine()) != null) {
  25. // Perform your validation checks on each line here
  26. if (!isValidLine(line)) {
  27. System.err.println("Validation failed on line " + lineNumber + ": " + line);
  28. validationSuccessful = false;
  29. }
  30. lineNumber++;
  31. }
  32. if (validationSuccessful) {
  33. System.out.println("Log file " + logFile + " validation successful.");
  34. } else {
  35. System.err.println("Log file " + logFile + " validation failed.");
  36. }
  37. }
  38. }
  39. private static boolean isValidLine(String line) {
  40. // Replace this with your actual validation logic
  41. // This is just a placeholder example - check if line contains "ERROR"
  42. return line.contains("ERROR");
  43. }
  44. }

Add your comment