Log Levels and Attributes
Add context to logs.
Log Levels and Attributes is a free Go Academy lesson on CoddyKit — lesson 3 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.
Adding Context to Logs
A bare message tells you what happened; attributes tell you the details that make logs actionable: which user, which request, how long it took.
The Four Levels
slog levels, lowest to highest severity:
Debug(-4)Info(0)Warn(4)Error(8)
Levels are integers, so you can define custom ones in between.
Filtering by Level
Set a minimum level in HandlerOptions. Records below it are dropped cheaply.
package main
import (
"log/slog"
"os"
)
func main() {
opts := &slog.HandlerOptions{Level: slog.LevelWarn}
logger := slog.New(slog.NewTextHandler(os.Stdout, opts))
logger.Info("ignored")
logger.Warn("shown")
}Typed Attributes
Beyond loose key-value args, slog offers typed constructors that avoid mistakes and are faster:
slog.String("k", v)slog.Int("k", v)slog.Bool("k", v)slog.Duration("k", d)
slog.Info("done", slog.String("phase", "build"), slog.Int("files", 12))Why Typed Attrs
Typed attrs catch the "odd number of args" bug at compile time and let the handler skip reflection. Prefer them in hot paths.
Grouping Attributes
slog.Group nests related fields under one key.
slog.Info("req",
slog.Group("http",
slog.String("method", "GET"),
slog.Int("status", 200),
),
)Logger.With for Shared Context
logger.With(...) returns a child logger that attaches the same attributes to every message. Perfect for a request ID.
reqLog := logger.With("requestID", "abc-123")
reqLog.Info("started")
reqLog.Info("finished")Context-Aware Logging
The *Context variants (e.g. InfoContext) carry a context.Context, letting handlers extract trace IDs.
slog.InfoContext(ctx, "handling", "path", "/api")Dynamic Levels
Use a slog.LevelVar to change the level at runtime (e.g., bump to Debug when investigating an incident) without restarting.
var lvl slog.LevelVar
lvl.Set(slog.LevelInfo)
h := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: &lvl})
// later: lvl.Set(slog.LevelDebug)Errors as Attributes
Attach errors as attributes so they are queryable, rather than concatenating into the message.
slog.Error("save failed", slog.String("err", err.Error()), slog.Int("retry", 2))Runnable: Levels and With
This program filters by level and uses a child logger with shared context.
package main
import (
"log/slog"
"os"
)
func main() {
opts := &slog.HandlerOptions{Level: slog.LevelInfo}
base := slog.New(slog.NewTextHandler(os.Stdout, opts))
req := base.With("requestID", "r-7")
req.Debug("hidden")
req.Info("processing", slog.Int("items", 3))
}Quick Check
Test your understanding of levels and attributes.
Recap
You learned levels and attributes:
- Four built-in levels; filter with
HandlerOptions.Level - Typed attrs (
slog.String,slog.Int) are safer and faster slog.Groupnests fields;logger.Withshares contextLevelVarenables runtime level changes
Frequently asked questions
Is the “Log Levels and Attributes” lesson free?
Yes — the full text of “Log Levels and Attributes” 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 “Log Levels and Attributes”?
Add context to logs. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Log Levels and Attributes” 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