Go structured logging records events as key and value pairs, so both people and tools can read your logs easily. In this post, you will use the log/slog package to write text and JSON logs. You will also filter by level and attach request details. Also, each idea comes with a short example.
Above all, logs matter in servers, like the one from the Go HTTP server tutorial. We will also attach a request ID, an idea you met in Go context for beginners.
What Is Go Structured Logging?
A plain log line is just a sentence, such as “user asha logged in”. That is easy to read but hard to search. Structured logs, however, keep each detail in its own named field.
For example, think of a messy note versus a filled-in form. With a form, you can instantly find every row where user=asha. Since Go 1.21, the standard library includes log/slog for exactly this job, so you need no extra package.
Your First slog Message
slog.Info("server started", "port", 8080)
// 2026/08/21 09:00:00 INFO server started port=8080
First of all, the first argument is the message. After that, arguments come in pairs: a key, then its value. By default, slog writes through the classic log package, so the line starts with the date.
Text and JSON Handlers
In short, a handler decides how each record looks and where it goes. The diagram below shows one log call flowing through a logger into two different handlers.

The logger builds a record with a level, message and attributes, and the handler turns it into text or JSON.
text := slog.New(slog.NewTextHandler(os.Stdout, nil))
text.Info("user login", "user", "asha", "attempt", 1)
// time=... level=INFO msg="user login" user=asha attempt=1
jsonLog := slog.New(slog.NewJSONHandler(os.Stdout, nil))
jsonLog.Info("order placed", "order_id", 1042, "amount", 499.5)
// {"time":"...","level":"INFO","msg":"order placed","order_id":1042,"amount":499.5}
Also, the time is shortened to dots here. Use the text handler on your laptop, because it is easy to read. In production, prefer JSON, since log tools such as Grafana Loki or Elasticsearch can search every field.
Log Levels in Go Structured Logging
| Level | Use it for |
|---|---|
| Debug | Details only developers need |
| Info | Normal events, such as a server start |
| Warn | Something odd that did not fail |
| Error | A failure that needs attention |
jsonLog.Debug("not shown by default")
opts := &slog.HandlerOptions{Level: slog.LevelDebug}
debugLog := slog.New(slog.NewJSONHandler(os.Stdout, opts))
debugLog.Debug("cache miss", "key", "user:7")
// {"time":"...","level":"DEBUG","msg":"cache miss","key":"user:7"}
By default, handlers drop Debug messages. So pass HandlerOptions with a lower level when you need more detail, for example while hunting a bug.
Adding Context with With and Group
reqLog := jsonLog.With("request_id", "abc-123")
reqLog.Warn("slow query", "ms", 950)
// {"time":"...","level":"WARN","msg":"slow query","request_id":"abc-123","ms":950}
jsonLog.Info("request", slog.Group("http", "method", "GET", "status", 200))
// {"time":"...","level":"INFO","msg":"request","http":{"method":"GET","status":200}}
In particular, With returns a new logger that adds the same fields to every line. As a result, all logs for one request share its ID, which makes tracing easy. Meanwhile, Group nests related fields under one key.
Go Structured Logging in an HTTP Handler
func helloHandler(w http.ResponseWriter, r *http.Request) {
start := time.Now()
log := slog.With("method", r.Method, "path", r.URL.Path)
log.Info("request received")
w.Write([]byte("hello"))
log.Info("request done", "duration_ms", time.Since(start).Milliseconds())
}
// time=... level=INFO msg="request received" method=GET path=/hello
// time=... level=INFO msg="request done" method=GET path=/hello duration_ms=0
With Go structured logging, each handler builds a small logger that already knows the method and path. So every line from that request carries the same fields without repeating them. Also, in a real app, you would add the request ID here too. Then, when a user reports a problem, search for that ID to see the whole request story.
Setting the Default Logger
slog.SetDefault(jsonLog)
slog.Info("now JSON by default")
// {"time":"...","level":"INFO","msg":"now JSON by default"}
Finally, call SetDefault once in main. Then every package that uses slog.Info gets the same format. Indeed, this is the easiest way to switch a whole app to Go structured logging.
Common Mistakes with Go Structured Logging
| Problem you see | Why it happens | How to fix it |
|---|---|---|
!BADKEY= appears in the output | Because a key or value is missing in the pairs. | So always pass key, value, key, value. |
| Debug lines never show | Because the default level is Info. | So set Level: slog.LevelDebug. |
| Passwords appear in logs | Because secret fields were logged directly. | So never log secrets, or mask them. |
Also, run go vet to catch the first problem early. It reports errors such as “call to slog.Info missing a final value”. Logging questions also come up in backend interviews, so practise with my Go interview questions and answers.
Official Resources
These pages come straight from the Go team, so they stay accurate and up to date.
Quick Recap
- Go structured logging stores details as key and value pairs.
- The standard
log/slogpackage has text and JSON handlers. - Levels are Debug, Info, Warn and Error, with Info as the default.
Withadds shared fields, andGroupnests related ones.- Call
slog.SetDefaultonce to set the format for the whole app.
What’s Next?
As a result, your app now writes clear, searchable logs. Next, we will configure it without changing code by reading environment variables.
Next lesson: Go Environment Variables for App Configuration


