🐸

The Log Summarizer

The Data Tinkererpython-data-tinkerer-40-the-log-summarizer
Reward: 160 XP
|

The Log Summarizer

A teammate on the early shift just sent over a slice of app logs and wants a quick summary first, so the people checking and investigating next know where to look. Nobody is waiting for you to build a whole platform right now. They just want to know which rows deserve attention, which source is really failing, and which users appear in this chunk of activity.

So your job is to read this log sample clearly and turn it into a short, useful shift summary. Once you hand it back, the next person can decide much faster which lines to inspect further and which source deserves the first look.

First, look at one smaller move: split one log row and grab the key details

Do not rush into the whole log yet. Start with one toy row so you can see how the important information becomes clearer after the text is split apart.

toy_line = "2026-04-12 08:41 | WARN | gate | user=ava | badge scan delayed"

parts = toy_line.split(" | ")
timestamp = parts[0]
level = parts[1]
source = parts[2]
user = parts[3].replace("user=", "")
message = parts[4]
minute = timestamp.split(" ")[1]

print(f"{minute} {level} {source} {user} - {message}")

This example does not handle the full log or build the counts yet. It only demonstrates the first idea: a log is not one giant confusing text blob. It is a set of rows you can split, inspect, and condense.

Today’s deliverable: read village_app.log.txt and build a shift summary

The script already reads village_app.log.txt and stores the raw text in raw_log_text. You need to split it into rows, keep the alerts that actually need attention, count error sources, and place the result into alert_lines and log_summary.

1
Turn the raw log text into rows first

Use raw_log_text.splitlines() to build log_lines. That lets you process the log one row at a time instead of poking at the whole text blob.

2
Extract minute, level, source, user, and message from each row

Inside the loop, use line.split(" | ") to unpack the 5 fields. Then pull the minute string out of timestamp and clean the user name with replace("user=", "").

3
Keep only the rows that need immediate attention

If level is WARN or ERROR, turn that row into one short reminder and append it to alert_lines. If the level is ERROR, also count it by source inside error_counts.

4
Output the summary result

Use users_seen to keep users in first-seen order, then build log_summary with at least the total row count, alert count, error source counts, and user list.

Give people a usable first summary

A lot of investigation work begins exactly here: pull out the important rows first, then decide whether deeper digging is needed. That small handoff is what you are building now.

Suggested Solution
Expand
Solution:
ASSET_FILENAME = "village_app.log.txt"

with open(ASSET_FILENAME, "r", encoding="utf-8") as file:
  raw_log_text = file.read().strip()

print("Raw log text:")
print(raw_log_text)

log_lines = raw_log_text.splitlines()
alert_lines = []
error_counts = {}
users_seen = []

for line in log_lines:
  parts = line.split(" | ")
  timestamp = parts[0]
  level = parts[1]
  source = parts[2]
  user_part = parts[3]
  message = parts[4]

  minute = timestamp.split(" ")[1]
  user = user_part.replace("user=", "")

  if user not in users_seen:
      users_seen.append(user)

  if level == "WARN" or level == "ERROR":
      alert_lines.append(f"{minute} {level} {source} {user} - {message}")

  if level == "ERROR":
      if source not in error_counts:
          error_counts[source] = 0
      error_counts[source] += 1

log_summary = {
  "total_lines": len(log_lines),
  "alert_count": len(alert_lines),
  "error_counts": error_counts,
  "users_seen": users_seen,
}

print("Alert lines:", alert_lines)
print("Log summary:", log_summary)
Advanced Tips
Want more? Click to expand

The most important shift here is that you are not just splitting strings anymore. You are preparing the first useful summary that helps another person investigate.

Once you can pull alerts, sources, and users out of a record quickly, text processing has already started turning into a small real-world tool skill.

Loading...
Terminal
Terminal
Ready to run...