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.
Time.parse(string) converts timestamp string to Time object
Use regex to extract log components: /\[(.*?)\] \w+ (\S+) (\d+) (\d+)/
Hash.new(0) creates hash with default value 0 for counters
Hash.new { |h, k| h[k] = [] } creates hash with array defaults
time.strftime('%H:00') formats as '10:00'
Sort descending: array.sort_by { |item| -item[:key] }
Check status ranges: status >= 400 && status < 500
analyze_logs('access.log')
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
log = parse_log_line('[2024-01-15T10:00:15Z] INFO /api/users 200 45')
puts log[:path]
puts log[:status]
puts log[:response_time]
/api/users 200 45
analyze_logs('access.log')
puts 'Sorted OK'
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
Console output will appear here...
Are you sure?
You're making great progress
Become a Ruby Pro
1,600+ problems to master every concept