]> Cypherpunks.ru repositories - gostls13.git/blob - src/log/slog/example_test.go
log/slog: initial commit
[gostls13.git] / src / log / slog / example_test.go
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         "log/slog"
9         "log/slog/internal/testutil"
10         "net/http"
11         "os"
12         "time"
13 )
14
15 func ExampleGroup() {
16         r, _ := http.NewRequest("GET", "localhost", nil)
17         // ...
18
19         logger := slog.New(slog.HandlerOptions{ReplaceAttr: testutil.RemoveTime}.NewTextHandler(os.Stdout))
20         slog.SetDefault(logger)
21
22         slog.Info("finished",
23                 slog.Group("req",
24                         slog.String("method", r.Method),
25                         slog.String("url", r.URL.String())),
26                 slog.Int("status", http.StatusOK),
27                 slog.Duration("duration", time.Second))
28
29         // Output:
30         // level=INFO msg=finished req.method=GET req.url=localhost status=200 duration=1s
31 }