Challenge

Problem

Log analysis is fundamental for monitoring application health, debugging issues, and understanding system behavior. This drill teaches you to parse structured log files, extract key metrics, identify patterns and anomalies, and generate comprehensive reports. You'll learn log parsing, statistical analysis, time-series aggregation, and multi-dimensional reporting—essential skills for building monitoring tools, debugging production issues, and creating operational dashboards.

Difficulty: Intermediate

Instructions

  1. Read server logs from access.log
  2. Parse log format: [timestamp] level request_path status response_time
  3. Extract and analyze:
    • Request counts by HTTP status code
    • Average response time overall and by status code
    • Requests per hour
    • Top 5 slowest requests
    • Error rate percentage (4xx and 5xx)
  4. Print comprehensive report with all metrics

Files

Editable
Read-only

Hints

Hint 1

Time.parse(string) converts timestamp string to Time object

Hint 2

Use regex to extract log components: /\[(.*?)\] \w+ (\S+) (\d+) (\d+)/

Hint 3

Hash.new(0) creates hash with default value 0 for counters

Hint 4

Hash.new { |h, k| h[k] = [] } creates hash with array defaults

Hint 5

time.strftime('%H:00') formats as '10:00'

Hint 6

Sort descending: array.sort_by { |item| -item[:key] }

Hint 7

Check status ranges: status >= 400 && status < 500

Ruby 3.4

Provided Files (Read-only)

1. Correct log analysis report

Input:
analyze_logs('access.log')
Expected Output:
SERVER LOG ANALYSIS
==================================================
Log file: access.log
Total requests: 6
Time range: 2024-01-15T10:00:15Z to 2024-01-15T11:00:05Z
--------------------------------------------------
STATUS CODE DISTRIBUTION
200: 4 requests (66.67%)
404: 1 requests (16.67%)
500: 1 requests (16.67%)
--------------------------------------------------
RESPONSE TIME ANALYSIS
Average: 79.33ms
By Status Code:
  200: 65.25ms
  404: 12.00ms
  500: 203.00ms
--------------------------------------------------
REQUESTS PER HOUR
10:00 - 5 requests
11:00 - 1 requests
--------------------------------------------------
TOP 5 SLOWEST REQUESTS
/api/posts - 203ms
/api/users - 156ms
/api/users - 45ms
/api/posts - 32ms
/api/posts - 28ms
--------------------------------------------------
ERROR ANALYSIS
Error rate: 33.33%
4xx errors: 1 requests
5xx errors: 1 requests

2. parse_log_line extracts data

Input:
log = parse_log_line('[2024-01-15T10:00:15Z] INFO /api/users 200 45')
puts log[:path]
puts log[:status]
puts log[:response_time]
Expected Output:
/api/users
200
45

3. Status codes sorted numerically

Input:
analyze_logs('access.log')
puts 'Sorted OK'
Expected Output:
SERVER LOG ANALYSIS
==================================================
Log file: access.log
Total requests: 6
Time range: 2024-01-15T10:00:15Z to 2024-01-15T11:00:05Z
--------------------------------------------------
STATUS CODE DISTRIBUTION
200: 4 requests (66.67%)
404: 1 requests (16.67%)
500: 1 requests (16.67%)
--------------------------------------------------
RESPONSE TIME ANALYSIS
Average: 79.33ms
By Status Code:
  200: 65.25ms
  404: 12.00ms
  500: 203.00ms
--------------------------------------------------
REQUESTS PER HOUR
10:00 - 5 requests
11:00 - 1 requests
--------------------------------------------------
TOP 5 SLOWEST REQUESTS
/api/posts - 203ms
/api/users - 156ms
/api/users - 45ms
/api/posts - 32ms
/api/posts - 28ms
--------------------------------------------------
ERROR ANALYSIS
Error rate: 33.33%
4xx errors: 1 requests
5xx errors: 1 requests
Sorted OK
+ 2 hidden test cases