slog Handlers
Text and JSON handlers.
slog Handlers is a free Go Academy lesson on CoddyKit — lesson 2 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.
Handlers Decide the Format
In slog, a Logger calls a Handler to format and write each record. Swapping the handler changes the output format without touching your log calls.
TextHandler
slog.NewTextHandler writes human-friendly key=value lines. Good for local development and terminals.
package main
import (
"log/slog"
"os"
)
func main() {
h := slog.NewTextHandler(os.Stdout, nil)
logger := slog.New(h)
logger.Info("hello", "env", "dev")
}JSONHandler
slog.NewJSONHandler emits one JSON object per line. Ideal for production log pipelines.
package main
import (
"log/slog"
"os"
)
func main() {
h := slog.NewJSONHandler(os.Stdout, nil)
logger := slog.New(h)
logger.Info("hello", "env", "prod")
}Creating a Logger
slog.New(handler) wraps a handler in a Logger. You call logger.Info, logger.Error, etc. on that instance.
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))HandlerOptions
The second argument to a handler constructor is *slog.HandlerOptions. Pass nil for defaults, or configure level and source location.
opts := &slog.HandlerOptions{
Level: slog.LevelDebug,
AddSource: true,
}
h := slog.NewJSONHandler(os.Stdout, opts)Writing to a File or Buffer
Handlers take any io.Writer. You can log to a file, a buffer for tests, or stdout. This decoupling is a key slog design point.
f, _ := os.Create("app.log")
logger := slog.New(slog.NewJSONHandler(f, nil))Setting the Default Logger
slog.SetDefault(logger) makes the package-level functions (slog.Info etc.) use your handler. Set it once at startup.
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, nil)))
slog.Info("now JSON by default")Text vs JSON Output
Same call, two formats:
- Text:
time=... level=INFO msg=hello env=dev - JSON:
{"time":"...","level":"INFO","msg":"hello","env":"dev"}
Choosing a Handler
Rule of thumb:
- Local/dev: TextHandler (readable)
- Production/aggregated: JSONHandler (parseable)
Decide via an env var so the same binary adapts.
Writing a Reusable Setup
Centralize logger construction so the whole app shares one configuration.
func newLogger(prod bool) *slog.Logger {
if prod {
return slog.New(slog.NewJSONHandler(os.Stdout, nil))
}
return slog.New(slog.NewTextHandler(os.Stdout, nil))
}Runnable: Switch Handlers
This program logs the same event through both handlers so you can compare formats.
package main
import (
"log/slog"
"os"
)
func main() {
text := slog.New(slog.NewTextHandler(os.Stdout, nil))
js := slog.New(slog.NewJSONHandler(os.Stdout, nil))
text.Info("login", "user", "ada")
js.Info("login", "user", "ada")
}Quick Check
Test your understanding of slog handlers.
Recap
You learned about slog handlers:
- Handlers format and write records; the Logger calls them
NewTextHandlerfor humans,NewJSONHandlerfor machines- Handlers accept any
io.Writerand*HandlerOptions slog.SetDefaultwires the package-level functions
Frequently asked questions
Is the “slog Handlers” lesson free?
Yes — the full text of “slog Handlers” 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 “slog Handlers”?
Text and JSON handlers. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “slog Handlers” 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.