0Pricing
JavaScript Academy · Lesson

E2 — Log Analyzer (simple filters & counters)

Parse an in-memory array of log lines, filter by level/keyword, and count events with Map. Keep it tiny and readable.

What we will build

Goal: Analyze logs in memory.

  • Represent lines as tiny strings
  • Filter by level or keyword
  • Count with a Map
  • Keep functions short and clear
E2 — Log Analyzer (simple filters & counters) — illustration 1

Data + parser

Create a simple shape per line: time, level, text. It is enough for filters and counters.

// Simulated log lines (time level message)
const LINES = [
  "10:00 INFO  Start app",
  "10:01 WARN  Low memory",
  "10:02 INFO  User login /home",
  "10:03 ERROR DB timeout",
  "10:04 INFO  User login /products",
  "10:05 ERROR Network fail",
  "10:06 WARN  Retry request"
];

// Tiny parser: returns { time, level, text }
function parseLine(line) {
  // split on spaces, compact, then rebuild pieces
  const parts = line.trim().split(/\\s+/);
  if (parts.length < 3) return null;
  const time = parts[0];
  const level = parts[1];
  const text = parts.slice(2).join(" ");
  return { time: time, level: level, text: text };
}

console.log("parsed:", parseLine(LINES[2]));
E2 — Log Analyzer (simple filters & counters) — illustration 2

All lessons in this course

  1. E1 — Expression Evaluator (part 1: + and - LTR)
  2. E2 — Log Analyzer (simple filters & counters)
  3. E3 — Promise Pool/Queue (async concurrency)
← Back to JavaScript Academy