Go Academy · Lesson

slog Handlers

Text and JSON handlers.

Lesson 2 of 413 steps

slog Handlers is a free Go Academy lesson on CoddyKit. This is lesson 2 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.

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
  • NewTextHandler for humans, NewJSONHandler for machines
  • Handlers accept any io.Writer and *HandlerOptions
  • slog.SetDefault wires the package-level functions
Free to start

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 “slog Handlers” lesson free?

Yes — any 3 lessons of the Go Academy learning path, including “slog Handlers”, 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 “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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

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.

All lessons in this course

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