Part 08 / Topic 7 of 7
Putting it together: a simple log parser
A second, independent mini-build: read log lines shaped like timestamp|level|message, count
them by level, collect the errors, and skip (without crashing on) any line that doesn't fit the
shape -- a realistic worry for a log file nobody fully controls the format of.
class LogEntry(val timestamp: String, val level: String, val message: String)
fun parseLogLine(line: String): LogEntry {
val parts = line.split("|", limit = 3)
require(parts.size == 3) { "expected \"timestamp|level|message\", got \"$line\"" }
val level = parts[1].trim()
require(level in setOf("INFO", "WARN", "ERROR")) { "unknown level \"$level\"" }
return LogEntry(timestamp = parts[0].trim(), level = level, message = parts[2].trim())
}
fun writeSampleLog(file: File) {
val rawLines = listOf(
"2026-11-30 09:15:02|INFO|Order O-1001 placed",
"2026-11-30 09:15:05|INFO|Payment captured for O-1001",
"2026-11-30 09:16:40|WARN|Carrier API responded slowly (2.3s)",
"2026-11-30 09:17:10|ERROR|Carrier API timed out for O-1002",
"2026-11-30 09:18:00|INFO|Order O-1003 placed",
"this line is not a log entry at all",
"2026-11-30 09:19:45|ERROR|Payment declined for O-1004",
"2026-11-30 09:20:11|INFO|Order O-1003 shipped",
"2026-11-30 09:21:30|WARN|Retrying payment for O-1004"
)
file.writeText(rawLines.joinToString("\n") + "\n")
}
fun main() {
val logFile = File("app.log")
writeSampleLog(logFile)
val countsByLevel = mutableMapOf<String, Int>()
val errorEntries = mutableListOf<LogEntry>()
var skipped = 0
logFile.forEachLine { rawLine ->
if (rawLine.isBlank()) return@forEachLine
try {
val entry = parseLogLine(rawLine)
countsByLevel[entry.level] = (countsByLevel[entry.level] ?: 0) + 1
if (entry.level == "ERROR") {
errorEntries.add(entry)
}
} catch (e: IllegalArgumentException) {
skipped++
println("Skipped unparseable line: ${e.message}")
}
}
println()
println("--- Summary ---")
for (level in listOf("INFO", "WARN", "ERROR")) {
println("$level: ${countsByLevel[level] ?: 0}")
}
println("Skipped: $skipped")
println()
println("--- Errors ---")
for (entry in errorEntries) {
println("${entry.timestamp}: ${entry.message}")
}
}
Skipped unparseable line: expected "timestamp|level|message", got "this line is not a log entry at all"
--- Summary ---
INFO: 4
WARN: 2
ERROR: 2
Skipped: 1
--- Errors ---
2026-11-30 09:17:10: Carrier API timed out for O-1002
2026-11-30 09:19:45: Payment declined for O-1004
return@forEachLine (a labeled return, matching the lambda it's inside) skips just the
current line and moves on to the next one -- it does not stop the whole forEachLine call, the
same way continue skips one iteration of a for loop without ending it. The try/catch
around each line's parsing means one bad line produces a message and a skipped count, not a
crash that loses every line after it.
Full programs: examples/07_log_parser.kt (this one also writes its own sample app.log before
parsing it, so the example is fully self-contained).
Done here? The practice set for Part 8 — including the solutions — is on the part page.