// Chapter 47 example: text and JSON handlers, a lower level, and With. // Run with: go run exercises/47/02-handlers.go package main import ( "log/slog" "os" ) func main() { // key=value text, one line per entry. text := slog.New(slog.NewTextHandler(os.Stderr, nil)) text.Info("note added", "id", 3, "length", 12) // The same entry as JSON, for programs that read logs. asJSON := slog.New(slog.NewJSONHandler(os.Stderr, nil)) asJSON.Info("note added", "id", 3, "length", 12) // Debug entries appear only when the level is set to Debug. options := &slog.HandlerOptions{Level: slog.LevelDebug} debug := slog.New(slog.NewTextHandler(os.Stderr, options)) debug.Debug("config loaded", "file", "notes.json") // With returns a logger that adds the same pairs to every entry. request := text.With("method", "POST", "path", "/notes") request.Info("request started") request.Warn("the note is empty") }