-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
96 lines (81 loc) · 2.57 KB
/
Copy pathexample_test.go
File metadata and controls
96 lines (81 loc) · 2.57 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
package slogx_test
import (
"bytes"
"context"
"fmt"
"log/slog"
"time"
"github.com/Arhius/slogx"
)
// ExampleNew shows the zero-configuration path: a JSON logger at info level
// writing to [os.Stdout] and installed as [slog.Default]. The example does not
// include an Output block because the emitted record carries a non-
// deterministic "time" field.
func ExampleNew() {
lg := slogx.New(
slogx.WithLoadFromEnv(false),
slogx.WithSetDefault(false),
)
lg.Info("hello", "k", "v")
}
// ExampleNew_withLevelVar demonstrates flipping the [slog.LevelVar] threshold
// at runtime to change which records are emitted.
func ExampleNew_withLevelVar() {
lv := &slog.LevelVar{}
lv.Set(slog.LevelWarn)
lg := slogx.New(
slogx.WithLevelVar(lv),
slogx.WithLoadFromEnv(false),
slogx.WithSetDefault(false),
)
lg.Info("filtered") // below the threshold, dropped
lv.Set(slog.LevelDebug) // flip the threshold
lg.Info("emitted") // now emitted
}
// ExampleNew_withHandler shows how to plug in a caller-supplied
// [slog.Handler]. The custom handler owns its own level, format, and
// destination; LOG_* environment variables that feed the default handler are
// ignored.
func ExampleNew_withHandler() {
buf := &bytes.Buffer{}
h := slog.NewTextHandler(buf, &slog.HandlerOptions{Level: slog.LevelDebug})
lg := slogx.New(
slogx.WithHandler(h),
slogx.WithLoadFromEnv(false),
slogx.WithSetDefault(false),
)
// Emit a record with a zero time so the output is deterministic.
r := slog.NewRecord(time.Time{}, slog.LevelDebug, "hello", 0)
r.AddAttrs(slog.String("k", "v"))
_ = lg.Handler().Handle(context.Background(), r)
fmt.Print(buf.String())
// Output: level=DEBUG msg=hello k=v
}
// ExampleNew_textFormat shows how to switch from the default JSON encoder to
// the text encoder without providing a custom handler.
func ExampleNew_textFormat() {
buf := &bytes.Buffer{}
lg := slogx.New(
slogx.WithWriter(buf),
slogx.WithFormat(slogx.TextFormat),
slogx.WithLoadFromEnv(false),
slogx.WithSetDefault(false),
)
r := slog.NewRecord(time.Time{}, slog.LevelInfo, "hello", 0)
r.AddAttrs(slog.String("k", "v"))
_ = lg.Handler().Handle(context.Background(), r)
fmt.Print(buf.String())
// Output: level=INFO msg=hello k=v
}
// ExampleNewWithLevelVar shows runtime level flipping without allocating a
// [*slog.LevelVar] up front.
func ExampleNewWithLevelVar() {
lg, lv := slogx.NewWithLevelVar(
slogx.WithLevel(slogx.WarnLevel),
slogx.WithLoadFromEnv(false),
slogx.WithSetDefault(false),
)
lg.Info("filtered") // below warn, dropped
lv.Set(slog.LevelDebug)
lg.Info("emitted")
}