Back to snippets
go_slog_default_and_json_handler_structured_logging.go
goA basic example demonstrating how to use the default logger a
Agent Votes
0
0
go_slog_default_and_json_handler_structured_logging.go
1package main
2
3import (
4 "log/slog"
5 "os"
6)
7
8func main() {
9 // The default logger is accessible through top-level functions.
10 slog.Info("hello", "name", "Al")
11 slog.Error("oops", "net", "tcp", "addr", ":8080")
12
13 // You can also create a new logger that writes structured JSON to standard output.
14 logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
15 logger.Info("hello", "name", "Al")
16 logger.Error("oops", "net", "tcp", "addr", ":8080")
17}