-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.go
37 lines (29 loc) · 881 Bytes
/
example.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package cslog
import (
"context"
"log/slog"
"os"
"time"
)
func ExampleCtx() {
getFromDB := func(ctx context.Context, userID string) int {
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
ctx = WithAttrs(ctx, slog.String("userID", userID))
// call DB
const fakeResult = 42
Debug(ctx, "got from DB", slog.Int("result", 42))
return fakeResult
}
var opt *slog.HandlerOptions
// uncomment below to have the debug show up!
// opt = &slog.HandlerOptions{Level: slog.LevelDebug}
ctx := FromBackground(slog.New(slog.NewJSONHandler(os.Stdout, WithoutTime(opt))))
Info(ctx, "hello!")
y, _, _ := time.Now().Local().Date()
ctx = WithAttrs(ctx, slog.Int("year", y))
got := getFromDB(WithGroup(ctx, "getFromDB"), "foobar!!!")
if want := 43; got != want {
Error(ctx, "wrong result!", slog.Int("wanted", want), slog.Int("got", got))
}
}