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.

E2 — Log Analyzer (simple filters & counters) is a free JavaScript Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

Filtering basics

Write tiny filter functions for each common question. Short and readable beats clever.

// Filter by level or by keyword (case-sensitive for simplicity)
function filterByLevel(lines, want) {
  const out = [];
  for (const line of lines) {
    const p = parseLine(line);
    if (p && p.level === want) out.push(p);
  }
  return out;
}

function filterByKeyword(lines, word) {
  const out = [];
  for (const line of lines) {
    const p = parseLine(line);
    if (p && p.text.indexOf(word) !== -1) out.push(p);
  }
  return out;
}

console.log("ERRORs:", filterByLevel(LINES, "ERROR").length);
console.log("login :", filterByKeyword(LINES, "login").length);
E2 — Log Analyzer (simple filters & counters) — illustration 3

Counters with Map

Use a Map for counters: fast updates and easy reads per key.

// Count how many lines per level
function countByLevel(lines) {
  const counts = new Map();
  for (const line of lines) {
    const p = parseLine(line);
    if (!p) continue;
    const old = counts.get(p.level) || 0;
    counts.set(p.level, old + 1);
  }
  return counts;
}

const c = countByLevel(LINES);
console.log("INFO :", c.get("INFO"));
console.log("WARN :", c.get("WARN"));
console.log("ERROR:", c.get("ERROR"));
E2 — Log Analyzer (simple filters & counters) — illustration 4

Top-N example

Do small string extraction and rank with a count Map. Keep parsing rules simple.

// Extract a path after "User login " and rank by frequency
function topLoginPaths(lines, k) {
  const freq = new Map();
  for (const line of lines) {
    const p = parseLine(line);
    if (!p) continue;
    const mark = "User login ";
    const idx = p.text.indexOf(mark);
    if (idx !== -1) {
      const path = p.text.slice(idx + mark.length).trim();
      const old = freq.get(path) || 0;
      freq.set(path, old + 1);
    }
  }
  // turn into array and sort descending by count
  const arr = Array.from(freq.entries());
  arr.sort(function (a, b) { return b[1] - a[1]; });
  return arr.slice(0, k);
}

console.log("top paths:", topLoginPaths(LINES, 3));
E2 — Log Analyzer (simple filters & counters) — illustration 5

Beginner guidance

Tips:

  • Make a tiny parser first.
  • Write small filter helpers per question.
  • Count with a Map.
  • Prefer simple strings and clear loops for beginners.
E2 — Log Analyzer (simple filters & counters) — illustration 6

Counting with Map quiz

Quick check: Counting structure.

E2 — Log Analyzer (simple filters & counters) — illustration 7

Recap

Recap: Parse to a tiny shape, filter by level/keyword, and count with a Map. Keep each helper short and obvious.

E2 — Log Analyzer (simple filters & counters) — illustration 8

Frequently asked questions

Is the “E2 — Log Analyzer (simple filters & counters)” lesson free?

Yes — the full text of “E2 — Log Analyzer (simple filters & counters)” is free to read here on the web, and the JavaScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “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. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “E2 — Log Analyzer (simple filters & counters)” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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