0PricingLogin
Go Academy · Lesson

slog Handlers

Text and JSON handlers.

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")
}

All lessons in this course

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