Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
105 lines (79 loc) · 3.54 KB
/
Copy pathexample_test.go
File metadata and controls
105 lines (79 loc) · 3.54 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package clog_test
import (
"context"
"log/slog"
"os"
"time"
"github.com/telepresenceio/clog"
"github.com/telepresenceio/clog/handler"
"github.com/telepresenceio/clog/internal"
)
func fakeTime() {
internal.TimeNow = func() time.Time { return time.Date(2026, 1, 2, 3, 4, 5, 678900000, time.UTC) }
}
func ExampleInfof() {
fakeTime()
lg := slog.New(slog.NewTextHandler(os.Stdout, nil))
ctx := clog.WithLogger(context.Background(), lg)
clog.Infof(ctx, "Hello, %s!", "world")
// Output:
// time=2026-01-02T03:04:05.678Z level=INFO msg="Hello, world!"
}
func ExampleNewText() {
lg := slog.New(handler.NewText(handler.TimeFormat(""), handler.EnabledLevel(slog.LevelInfo), handler.HideLevel(slog.LevelWarn)))
ctx := clog.WithLogger(context.Background(), lg)
clog.Infof(ctx, "Hello, %s!", "world")
ctx = clog.WithGroup(ctx, "first")
clog.Infof(ctx, "Hello, %s!", "world")
ctx = clog.WithGroup(ctx, "second")
clog.Infof(ctx, "Hello, nested %s!", "world")
clog.Warn(ctx, "This is a warning!")
// Output:
// INFO Hello, world!
// INFO first: Hello, world!
// INFO first/second: Hello, nested world!
// first/second: This is a warning!
}
func ExampleNewText_levelEnabler() {
type lvlKey struct{}
ctx := clog.WithTreeLevel(context.Background(), slog.LevelInfo)
lg := slog.New(handler.NewText(handler.TimeFormat(""), handler.LevelEnabler(clog.TreeEnabled), handler.HideLevel(slog.LevelWarn)))
ctx = clog.WithLogger(ctx, lg)
clog.Info(ctx, "Hello, info!")
clog.Debug(ctx, "Hello, debug!")
clog.SetTreeLevel(ctx, slog.LevelDebug)
clog.Info(ctx, "Hello, info!")
clog.Debug(ctx, "Hello, debug!")
// Output:
// INFO Hello, info!
// INFO Hello, info!
// DEBUG Hello, debug!
}
func ExampleInfof_withAttrsAndGroups() {
fakeTime()
lg := slog.New(handler.NewText(handler.TimeFormat("15:04:05.0000"), handler.EnabledLevel(slog.LevelInfo)))
topCtx := clog.WithLogger(context.Background(), lg)
clog.Debug(topCtx, "Hello, world!")
clog.Infof(topCtx, "Hello, %s!", "world")
ctx := clog.WithGroup(topCtx, "group")
clog.Infof(ctx, "Hello, %s!", "world")
clog.Info(topCtx, "Hello, world!", slog.Group("group", slog.Group("hello", "this", "value")))
clog.Info(topCtx, "Hello, world!", slog.GroupAttrs("group", slog.String("this", "value"), slog.Group("hello", "that", "thing", "is", `so "cool"`)))
clog.Info(topCtx, "Hello, world!", slog.GroupAttrs("group", slog.String("this", "value"), slog.Group("hello", slog.Group("that", "thing", "is cool"))))
clog.Info(clog.With(topCtx, "what", "world"), "Hello!")
clog.Info(topCtx, "Hello, world!", slog.String("this", "value"), slog.Group("hello", slog.Group("that", "thing", "is cool")))
clog.Infof(ctx, "Hello, world! %s", slog.GroupAttrs("group", slog.String("this", "value"), slog.Group("hello", slog.Group("that", "thing", `is "cool"`))))
clog.Infof(ctx, "Hello, world! %s", slog.Float64("value", 2.24))
clog.Infof(ctx, "Hello, world! value: %.3f", 2.24)
// Output:
// 03:04:05.6789 INFO Hello, world!
// 03:04:05.6789 INFO group: Hello, world!
// 03:04:05.6789 INFO group/hello: Hello, world! : this=value
// 03:04:05.6789 INFO group: Hello, world! : this=value hello={that=thing is="so \"cool\""}
// 03:04:05.6789 INFO group: Hello, world! : this=value hello/that={thing="is cool"}
// 03:04:05.6789 INFO Hello! : what=world
// 03:04:05.6789 INFO Hello, world! : this=value hello/that={thing="is cool"}
// 03:04:05.6789 INFO group: Hello, world! group=[this=value hello=[that=[thing=is "cool"]]]
// 03:04:05.6789 INFO group: Hello, world! value=2.24
// 03:04:05.6789 INFO group: Hello, world! value: 2.240
}