0Pricing
Go Academy · Lesson

Why Structured Logs

Machine-readable logging.

Why Structured Logs is a free Go Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Logs Machines Can Read

Traditional logs are free-form strings: "user 42 logged in from 1.2.3.4". They are easy for humans but painful for machines to query. Structured logging emits key-value pairs so log aggregators can filter and aggregate reliably.

The Problem With String Logs

Parsing free text is brittle:

  • Formats drift over time
  • Regexes break on edge cases
  • You cannot easily ask "all events where userID=42"

Structured logs make every field a first-class, queryable attribute.

slog Is in the Standard Library

Since Go 1.21, structured logging lives in the standard library package log/slog. No external dependency needed.

import "log/slog"

Your First Structured Log

The simplest call logs a message plus key-value attributes:

package main

import "log/slog"

func main() {
    slog.Info("user logged in", "userID", 42, "ip", "1.2.3.4")
}

Reading the Output

The default handler prints something like:

  • 2009/11/10 23:00:00 INFO user logged in userID=42 ip=1.2.3.4

Each attribute is a discrete key=value pair, ready for machine parsing.

Levels Built In

slog ships four levels: Debug, Info, Warn, Error. Each takes a message and alternating key-value attributes.

slog.Warn("disk almost full", "freePct", 5)
slog.Error("db connection failed", "attempt", 3)

Key-Value Pairs

Attributes are passed as alternating arguments: key, value, key, value. A stray odd argument is logged with a special !BADKEY marker, so keep them paired.

slog.Info("order placed", "orderID", 1001, "total", 49.99)

JSON for Production

In production you usually emit JSON so log shippers (Loki, ELK, CloudWatch) can index fields automatically. slog has a JSON handler for exactly this, covered next lesson.

Comparing to the Old log Package

The classic log package only prints strings. slog keeps a familiar feel but adds structure, levels, and pluggable handlers without changing your whole codebase at once.

When Structure Pays Off

Structured logging pays off most when:

  • You centralize logs from many services
  • You build dashboards and alerts on fields
  • You need to correlate requests by an ID

Runnable: Structured Output

Run this to see structured attributes in the default output.

package main

import "log/slog"

func main() {
    slog.Info("request handled",
        "method", "GET",
        "path", "/health",
        "status", 200,
        "ms", 12,
    )
}

Quick Check

Test your understanding of structured logging.

Recap

You learned why structured logs matter:

  • Key-value attributes are machine-queryable
  • log/slog is in the standard library (Go 1.21+)
  • Built-in levels: Debug, Info, Warn, Error
  • Pass attributes as alternating key, value pairs

Next: choosing text vs JSON handlers.

Frequently asked questions

Is the “Why Structured Logs” lesson free?

Yes — the full text of “Why Structured Logs” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “Why Structured Logs”?

Machine-readable logging. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Why Structured Logs” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why Structured Logs
  2. slog Handlers
  3. Log Levels and Attributes
  4. Custom Handlers
← Back to Go Academy