ProgramGuru

Log File Analyzer

Start with Log File Analyzer

Learn log file analyzer through an executable example, then connect the small demonstration to production code.

What you’ll learn

  • Explain the main purpose of log file analyzer.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

A log analyzer scans lines, extracts severity levels or structured fields, and produces a useful summary. Keep parsing and presentation separate so malformed lines can be handled explicitly.

This example analyzes a small in-memory log fixture; replace the reader with an actual file for a CLI tool.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for log analyzerSeparate parsing failures from normal log entries.Read log linesStep 1Parse levelStep 2Count occurrencesStep 3Summarize resultsStep 4Separate parsing failures from normal log entries.
Separate parsing failures from normal log entries.

Tools, syntax and practical notes

Extensions: accept a filename via flags, stream instead of loading an entire large file, support JSON log entries, allow time-range filtering and test malformed records.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import (
	"bufio"
	"fmt"
	"strings"
)

func main() {
	input := "INFO booted\nWARN retrying\nERROR disk full\nERROR failed write\n"
	counts := map[string]int{}
	scanner := bufio.NewScanner(strings.NewReader(input))
	for scanner.Scan() {
		fields := strings.Fields(scanner.Text())
		if len(fields) > 0 {
			counts[fields[0]]++
		}
	}
	if err := scanner.Err(); err != nil {
		panic(err)
	}
	for _, level := range []string{"INFO", "WARN", "ERROR"} {
		fmt.Println(level, counts[level])
	}
}

Output

INFO 1
WARN 1
ERROR 2

Trace what the program does

The scanner reads four lines and counts the first token on each. Printing levels in a fixed list avoids depending on map order.

Avoid common mistakes

  • Scanner has a default token-size limit; raise its buffer size for large log lines when required.
  • Real logs may be JSON or use formats in which the first token is not the severity level.

Try it yourself

Parse newline-delimited JSON records, count errors by component, and report malformed lines separately.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

Why iterate over a fixed list of severity levels when printing?
Choose one answer. You can try again.

Answer and explanation

To avoid relying on map iteration order

Correct. The scanner reads four lines and counts the first token on each.

What you should remember

You have practiced Log File Analyzer in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: HTTP File Server →