Part 07 / Topic 7 of 7
Putting it together: a simple log parser
Real-world example: a store's order log, one line per event, that a support engineer needs summarized — how many INFO/WARN/ERROR lines, and the ERROR lines in full. This is the part's second "you will build" item: a single packaged class that reads a file line by line and counts.
logparser/sample.log:
2026-11-20 09:12:05 INFO Order 1042 placed by Asha for ₹1760
2026-11-20 09:14:47 WARN Payment retry for order 1043
2026-11-20 09:15:02 ERROR Payment failed for order 1043
2026-11-20 09:20:11 INFO Order 1044 placed by Ravi for ₹540
2026-11-20 09:25:33 ERROR Inventory mismatch for SKU-22
2026-11-20 09:30:00 INFO Order 1045 placed by Meera for ₹2999
2026-11-20 09:32:10 WARN Low stock for SKU-11
2026-11-20 09:40:18 INFO Order 1046 placed by Asha for ₹999
2026-11-20 09:45:59 ERROR Payment failed for order 1046
2026-11-20 09:50:21 INFO Order 1047 placed by Ravi for ₹1500
logparser/LogParser.java:
package logparser;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
public class LogParser {
public static void main(String[] args) {
Path logFile = args.length > 0 ? Path.of(args[0]) : Path.of("logparser/sample.log");
Map<String, Integer> countsByLevel = new HashMap<>();
int totalLines = 0;
System.out.println("---- ERROR lines ----");
try (BufferedReader reader = Files.newBufferedReader(logFile)) {
String line;
while ((line = reader.readLine()) != null) {
if (line.isBlank()) {
continue;
}
totalLines++;
// "2026-11-20 09:15:02 ERROR Payment failed for order 1043"
String[] parts = line.split(" ", 4);
String level = parts[2];
countsByLevel.put(level, countsByLevel.getOrDefault(level, 0) + 1);
if (level.equals("ERROR")) {
System.out.println(line);
}
}
} catch (IOException e) {
System.out.println("Could not read log file " + logFile + ": " + e.getMessage());
return;
}
System.out.println("---- summary ----");
System.out.println("Total lines: " + totalLines);
for (String level : new String[] {"INFO", "WARN", "ERROR"}) {
System.out.println(level + ": " + countsByLevel.getOrDefault(level, 0));
}
}
}
line.split(" ", 4) splits on spaces but stops after 3 splits, so the message (which itself contains spaces) stays in one piece as the 4th element — only the date, time and level need to be pulled apart on their own.
The level counts are printed by looping over a fixed array ("INFO", "WARN", "ERROR") instead of over the map's own keys. A HashMap's key order is not guaranteed to be the insertion order or any particular order — looping over a list of known keys you chose yourself is how you get output that is the same on every run and every machine.
$ javac -d out logparser/LogParser.java
$ java -cp out logparser.LogParser
---- ERROR lines ----
2026-11-20 09:15:02 ERROR Payment failed for order 1043
2026-11-20 09:25:33 ERROR Inventory mismatch for SKU-22
2026-11-20 09:45:59 ERROR Payment failed for order 1046
---- summary ----
Total lines: 10
INFO: 5
WARN: 2
ERROR: 3
Full program: examples/log-parser/.
Done here? The practice set for Part 7 — including the solutions — is on the part page.