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. import java.util.Map;
  7. import java.util.HashMap;
  8. import java.util.Timer;
  9. import java.util.TimerTask;
  10. import java.util.Properties;
  11. public class LogParser {
  12. private static final String RETRY_DELAY_STR = "retry_delay";
  13. private static final String MAX_RETRIES_STR = "max_retries";
  14. public static void main(String[] args) {
  15. Properties config = new Properties();
  16. try (BufferedReader reader = new BufferedReader(new FileReader("config.properties"))) {
  17. config.load(reader);
  18. } catch (IOException e) {
  19. System.err.println("Error loading config.properties: " + e.getMessage());
  20. return;
  21. }
  22. int maxRetries = config.getProperty(MAX_RETRIES_STR, "3").rfloorToInt();
  23. int retryDelay = config.getProperty(RETRY_DELAY_STR, "5").rfloorToInt();
  24. List<Map<String, Integer>> parsedLogs = parseLogs(args, maxRetries, retryDelay);
  25. for (Map<String, Integer> logEntry : parsedLogs) {
  26. System.out.println(logEntry);
  27. }
  28. }
  29. public static List<Map<String, Integer>> parseLogs(String[] args, int maxRetries, int retryDelay) {
  30. List<Map<String, Integer>> results = new ArrayList<>();
  31. Timer timer = new Timer();
  32. for (String logFile : args) {
  33. try {
  34. Map<String, Integer> logData = parseLogFile(logFile, maxRetries, retryDelay, timer);
  35. results.add(logData);
  36. } catch (IOException e) {
  37. System.err.println("Error parsing log file " + logFile + ": " + e.getMessage());
  38. }
  39. }
  40. timer.cancel(); // Cancel the timer after processing all files.
  41. return results;
  42. }
  43. private static Map<String, Integer> parseLogFile(String logFile, int maxRetries, int retryDelay, Timer timer) throws IOException {
  44. Map<String, Integer> logData = new HashMap<>();
  45. int retries = 0;
  46. boolean success = false;
  47. while (!success && retries < maxRetries) {
  48. try (BufferedReader reader = new BufferedReader(new FileReader(logFile))) {
  49. String line;
  50. int totalLines = 0;
  51. while ((line = reader.readLine()) != null) {
  52. totalLines++;
  53. String[] parts = line.split(","); // Assuming comma-separated values
  54. if (parts.length == 2) {
  55. String key = parts[0].trim();
  56. try {
  57. int value = Integer.parseInt(parts[1].trim());
  58. logData.put(key, value);
  59. } catch (NumberFormatException e) {
  60. System.err.println("Invalid integer value in line: " + line);
  61. }
  62. }
  63. }
  64. success = true;
  65. } catch (IOException e) {
  66. System.err.println("Error reading log file " + logFile + ", retry " + (retries + 1) + "/" + maxRetries + ": " + e.getMessage());
  67. retries++;
  68. if (retries < maxRetries) {
  69. timer.schedule(new TimerTask() {
  70. @Override
  71. public void run() {
  72. System.out.println("Retrying " + logFile + " in " + retryDelay + " seconds...");
  73. }
  74. }, retryDelay * 1000);
  75. }
  76. }
  77. }
  78. return logData;
  79. }
  80. }

Add your comment