import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class LogPrinter {
public static void main(String[] args) {
String logFilePath = "default.log"; // Default log file path
Map<String, Integer> defaultValues = new HashMap<>();
defaultValues.put("counter", 0);
defaultValues.put("total", 0);
defaultValues.put("errorCount", 0);
try {
printLogFile(logFilePath, defaultValues);
} catch (IOException e) {
System.err.println("Error reading log file: " + e.getMessage());
}
}
public static void printLogFile(String logFilePath, Map<String, Integer> defaultValues) throws IOException {
String line;
BufferedReader reader = new BufferedReader(new FileReader(logFilePath));
List<Map<String, Integer>> logEntries = new ArrayList<>();
//Read the log file line by line
while ((line = reader.readLine()) != null) {
// Parse each line (assuming a simple key=value format)
String[] parts = line.split("=", 2);
if (parts.length == 2) {
String key = parts[0].trim();
String valueStr = parts[1].trim();
//Try to parse the value as an integer, use default if parsing fails
Integer value = Integer.parseInt(valueStr);
if(value != Integer.MIN_VALUE){
logEntries.add(new HashMap<>());
logEntries.get(logEntries.size() - 1).put(key, value);
} else {
logEntries.add(new HashMap<>());
logEntries.get(logEntries.size() - 1).put(key, defaultValues.get(key));
}
}
}
reader.close();
// Pretty print the results
printLogEntries(logEntries, defaultValues);
}
private static void printLogEntries(List<Map<String, Integer>> logEntries, Map<String, Integer> defaultValues) {
//Print Log Entries
for (Map<String, Integer> entry : logEntries) {
System.out.println("--------------------");
for (Map.Entry<String, Integer> pair : entry.entrySet()) {
System.out.println(pair.getKey() + ": " + pair.getValue());
}
}
}
}
Add your comment