golang.org/x/exp@v0.0.0-20240506185415-9bf2ced13842/slog/example_test.go (about) 1 // Copyright 2022 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package slog_test 6 7 import ( 8 "net/http" 9 "os" 10 "time" 11 12 "golang.org/x/exp/slog" 13 "golang.org/x/exp/slog/internal/testutil" 14 ) 15 16 func ExampleGroup() { 17 r, _ := http.NewRequest("GET", "localhost", nil) 18 // ... 19 20 logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: testutil.RemoveTime})) 21 slog.SetDefault(logger) 22 23 slog.Info("finished", 24 slog.Group("req", 25 slog.String("method", r.Method), 26 slog.String("url", r.URL.String())), 27 slog.Int("status", http.StatusOK), 28 slog.Duration("duration", time.Second)) 29 30 // Output: 31 // level=INFO msg=finished req.method=GET req.url=localhost status=200 duration=1s 32 }