Why Structured Logs
Machine-readable logging.
Why Structured Logs is a free Go Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, and your progress syncs across the web and the CoddyKit app. The Go Academy course includes 4 lessons in total.
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/slogis 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.
Learn Go with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Why Structured Logs” lesson free?
Yes — any 3 lessons of the Go Academy learning path, including “Why Structured Logs”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Go Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
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
- Why Structured Logs
- slog Handlers
- Log Levels and Attributes
- Custom Handlers