← All Exercises

Turn Log Lines into Structured Counts

basicjavascript~25min

Background

Logs are write-optimized and analysis-hostile. A small parser that emits counts and messages enables triage without shipping logs to a paid backend.

Problem

Parse key-value log lines and produce per-level counts plus a normalized message list so an incident reviewer can scan what happened.

Input

JSON object: { lines: string[] } where lines carry level=<x> and msg="..."

Output

JSON object: { counts: { [level]: number }, messages: [{ level, message }] }

Sample tests

Sample 1

Input: {"lines":["level=info msg=\"request served\"","level=error msg=\"db timeout\"","level=error msg=\"retry exhausted\""]}
Expected: {"counts":{"info":1,"error":2},"messages":[{"level":"info","message":"request served"},{"level":"error","message":"db timeout"},{"level":"error","message":"retry exhausted"}]}

Sample 2

Input: {"lines":["unstructured startup message"]}
Expected: {"counts":{"info":1},"messages":[{"level":"info","message":"unstructured startup message"}]}

Your solution

Learning objective

Convert raw log lines into counts and messages that support incident triage.