How Node really works · Chapter 31 of 34

Project: the log analyzer

By the end of this chapter you can turn a log file of any size into a short report, reading it one line at a time. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Projects
  • 6 tasks
  • Node.js
  • In the all labs plan

The first 2 chapters of How Node really works are free. This one comes with the all labs plan.

Why you need this

On Saturday the app was slow during the 4 pm rush, and three customers paid by UPI twice. Meera wants to know which part was slow. The order server writes every request to access.log, one line each, and after a busy week that file is a few hundred megabytes.

The first attempt was readFile, then split('\n'). It works on a test file. On the server it holds the whole week in memory, and the container has 256 MB: in the lab, a 144 MB log crashed it.

What you build: 6 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    parseLine: parse one access log line, and reject the broken ones

    Write parseLine(line). A good line, once spaces and line endings are trimmed from both ends, has exactly five parts separated by whitespace: time, method, path, status and duration, as in 2026-10-04T16:02:11.120Z GET /api/menu?lang=hi 200 34ms. The method is capital letters, the path starts with /, the status is exactly three digits and the duration is digits followed by ms.

    It returns { time, method, path, status, ms }, with path without its query string (/api/menu), status and ms as numbers (200 and 34). A line ending in \r is still good. For anything else (a blank line, fewer or more parts, a status such as OK, a duration such as slow) it returns null, and it never throws.

  2. Task 2

    readLines: stream a file line by line and close it when the reader stops

    Write readLines(file) as an async generator. It opens file with createReadStream from node:fs, reads it with readline, and yields each line without its line ending, in order. \r\n counts as one line ending, and a last line without a newline is still yielded (masala chai\r\ncutting chai\nbun maska gives three lines).

    It must not read the whole file first: on a big file, the first line arrives after only a small part has been read. When the caller leaves the loop early with break, the file stream is destroyed. Never read a .log file with readFile or readFileSync.

  3. Task 3

    analyzeLog: count entries, broken lines and status codes

    Write analyzeLog(file, { top }), with top 3 when it is left out. It reads the file through readLines and returns a promise of { total, malformed, statuses, slowest }. total is the number of lines parseLine accepts; malformed is the number of lines it rejects, but blank lines (empty or only spaces) are skipped and never counted; statuses is an object of status code to how many times it appeared, such as { '200': 4, '201': 1, '404': 1 }. slowest is task 4.

    A file with no lines gives total 0, malformed 0 and statuses {}.

  4. Task 4

    analyzeLog: rank the slowest endpoints by average time

    slowest in analyzeLog's result is an array of { path, avgMs, count } for the top endpoints: path is an entry's path (already without its query string), count is how many entries had it, and avgMs is the average of their ms, rounded to a whole number with Math.round. Sort by avgMs, highest first; when two have the same avgMs, by path A to Z. Keep only the first top of them.

    For example, /riders with 20ms and 40ms (avgMs 30), /menu with 30ms and /orders with 31ms, with top 2, give [{ path: '/orders', avgMs: 31, count: 1 }, { path: '/menu', avgMs: 30, count: 1 }]: /menu comes before /riders.

  5. Task 5

    writeReport: write the summary to a JSON report in a new folder

    Write writeReport(logFile, reportFile). It analyzes logFile with analyzeLog (default top), writes the summary to reportFile as JSON.stringify(summary, null, 2), and returns a promise of that same summary. The report's folder, such as reports/2026-10-04, may not exist yet: create it, with any missing parent folders, first.

  6. Task 6 · interview question

    Interview: analyzeLog, analyze a log too big to load at once

    analyzeLog must work on a log of any size by reading it one line at a time through readLines (createReadStream and readline), never with readFile or readFileSync, and keeping only counters, not lines. The check runs it on an 80,000-line log of about 4 MB with blank lines, broken lines and five kinds of request, and expects 50,000 entries, 10,000 malformed lines, the right status counts and the top three endpoints.

What you will learn

  • Parsing messy lines: never throwing on a broken one.
  • readline and async generators: one line at a time, and closing the file when the reader stops early.
  • Counting without keeping: totals and maps instead of lines.
  • Writing a report: a JSON file in a folder that does not exist yet.

More in Projects

Build "Project: the log analyzer" yourself

Tapri’s order server must never stall at the 4 pm rush. You learn why it would. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of How Node really works are free. This one comes with the all labs plan.