Project: a log analyzer

The tool

The first project takes one idea from most of the course at once: read a file, split a line, count in two hashes, sort, print. An access log in the common format is the input, and the output is the list of paths and status codes that a reader wants first.

lib/logstat.rb

class Logstat def initialize(lines) @lines = lines end def report paths = Hash.new(0) codes = Hash.new(0) @lines.each do |line| parts = line.split next if parts.length < 9 paths[parts[6]] += 1 codes[parts[8]] += 1 end { paths: paths.sort_by { |path, count| [-count, path] }.first(3).to_h, codes: codes.sort_by { |code, count| [-count, code] }.to_h } end end

The two index numbers are the ones that matter in this format: index 6 is the request path inside the quotes, index 8 is the status code after them. A line with fewer fields is skipped rather than guessed at.

bin/logstat

#!/usr/bin/env ruby require "json" require_relative "../lib/logstat" path = ARGV[0] || "access.log" report = Logstat.new(File.readlines(path)).report if ARGV.include?("--json") puts JSON.generate(report) else puts "Top paths:" report[:paths].each { |name, count| puts " #{name} #{count}" } puts "Status codes:" report[:codes].each { |code, count| puts " #{code} #{count}" } end

Run it on a log

A five-line sample is enough to see the shape. The same log format as the comparison lesson, with one path hit twice and two server errors:

access.log

127.0.0.1 - - [04/Nov/2026:10:00:00 +0000] "GET /index.html HTTP/1.1" 200 1024 127.0.0.1 - - [04/Nov/2026:10:00:01 +0000] "GET /index.html HTTP/1.1" 200 1024 127.0.0.1 - - [04/Nov/2026:10:00:02 +0000] "GET /boom HTTP/1.1" 500 0 127.0.0.1 - - [04/Nov/2026:10:00:03 +0000] "GET /missing HTTP/1.1" 404 12 127.0.0.1 - - [04/Nov/2026:10:00:04 +0000] "GET /slow HTTP/1.1" 500 0

ruby bin/logstat access.log

Top paths:
  /index.html 2
  /boom 1
  /missing 1
Status codes:
  200 2
  500 2
  404 1

The order is by count and then by name, so two runs on the same file print the same lines. That is worth the extra sort_by comparator: a report that reorders itself is hard to compare over time.

The report keeps only the three busiest paths, so the second server error, on /slow, stays out of the path list while the status count still sees it: two 500 lines and one 500 path is not a contradiction.

The JSON flag

A person wants a table; a pipeline wants data. The same report goes out as one JSON object when the flag is on:

ruby bin/logstat access.log --json

{"paths":{"/index.html":2,"/boom":1,"/missing":1},"codes":{"200":2,"500":2,"404":1}}

The test

The counting is the part that can be wrong, so the test builds three lines in memory and checks both hashes:

test/logstat_test.rb

require "minitest/autorun" require_relative "../lib/logstat" class LogstatTest < Minitest::Test def test_counts_paths_and_codes lines = [ '127.0.0.1 - - [04/Nov/2026:10:00:00 +0000] "GET /a HTTP/1.1" 200 1', '127.0.0.1 - - [04/Nov/2026:10:00:01 +0000] "GET /a HTTP/1.1" 500 0', '127.0.0.1 - - [04/Nov/2026:10:00:02 +0000] "GET /b HTTP/1.1" 200 1' ] report = Logstat.new(lines).report assert_equal({ "/a" => 2, "/b" => 1 }, report[:paths]) assert_equal({ "200" => 2, "500" => 1 }, report[:codes]) end end

ruby -Itest test/logstat_test.rb

Run options: --seed 12345

# Running:

.

Finished in 0.000848s, 1179.6404 runs/s, 2359.2807 assertions/s.

1 runs, 2 assertions, 0 failures, 0 errors, 0 skips

What to watch for

A log format is a contract, and this tool reads one fixed format. When the log is JSON or a custom line, the counting stays and the split changes; keep the split in one method so the test only has to change there.

A tool that reads a whole file fits a five-line sample and not a gigabyte. File.foreach with the same loop works line by line; the report is small either way.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does Hash.new(0) build?

    Answer

    A hash whose missing keys count as zero

    The default value is zero, so a counter can do paths[key] += 1 without a nil check.

  2. Why does the loop run next if parts.length < 9?

    Answer

    A line without the status field at index 8 is left out

    Index 8 is the status code; a line that does not reach it cannot be counted and is skipped, not guessed at.

  3. Why sort with [-count, path] instead of -count alone?

    Answer

    Paths with the same count come out in name order

    The first key orders by count, and the path breaks a tie, so the same log always prints the same list.

  4. What does the --json flag change?

    Answer

    The same report leaves as one JSON object instead of lines

    The counting is unchanged; only the output format is meant for a program instead of a person.

  5. What does this program print? paths = Hash.new(0) %w[/a /b /a].each { |p| paths[p] += 1 } p paths

    Answer

    {"/a" => 2, "/b" => 1}

    The default zero makes the first += work, and /a is seen twice.

  6. What does this program print? lines = ["a b c d e f /x g 200 h", "short"] paths = Hash.new(0) lines.each do |line| parts = line.split next if parts.length < 9 paths[parts[6]] += 1 end p paths

    Answer

    {"/x" => 1}

    The long line has index 6 equal to /x and the short line is skipped, so the count is one.

  7. What does this program print? rows = { "/b" => 1, "/a" => 1, "/c" => 2 } p rows.sort_by { |path, count| [-count, path] }

    Answer

    [["/c", 2], ["/a", 1], ["/b", 1]]

    The count comes first and descending, so /c leads; the two ones follow in path order.

  8. What does this program print? require "json" report = { paths: { "/a" => 2 }, codes: { "200" => 2 } } puts JSON.generate(report)

    Answer

    {"paths":{"/a":2},"codes":{"200":2}}

    JSON.generate writes symbols as strings and numbers as numbers, all on one line.

  9. A log line has only eight fields and the loop has no next guard. What goes wrong?

    Answer

    The line is counted under a nil path

    Index 8 is nil on a short line, so the count lands under nil and the report gains a key that is not a path.

  10. What does the report count if the path is read from parts[5] instead of parts[6]?

    Answer

    The HTTP method, such as "GET

    Index 5 is the quoted method at the start of the request; the path is the field after it, at index 6.

Article author: Arthur Isaev