-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
80 lines (68 loc) · 2.5 KB
/
Copy pathexample_test.go
File metadata and controls
80 lines (68 loc) · 2.5 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
package logger_test
import (
"context"
"os"
logger "github.com/ubgo/logger"
)
// Basic structured logging with typed, zero-allocation fields.
func Example() {
log := logger.New(
logger.WithLevel(logger.LevelInfo),
logger.WithSink(logger.NewWriterSink(os.Stdout, logger.NewJSONEncoder(), logger.LevelInfo)),
)
defer log.Close()
log.Info("server started",
logger.String("addr", ":8080"),
logger.Int("workers", 8),
)
}
// A child logger inherits bound fields; the parent is unaffected.
func ExampleLogger_With() {
log := logger.New()
reqLog := log.With(logger.String("request_id", "abc123"))
reqLog.Info("handling") // includes request_id
log.Info("global event") // does not
}
// FingersCrossed: a successful request emits nothing below the activation
// level; the first error flushes the whole buffered debug trail.
func ExampleFingersCrossed() {
fc := logger.NewFingersCrossed(
logger.NewWriterSink(os.Stdout, logger.NewJSONEncoder(), logger.LevelTrace),
)
log := logger.New(logger.WithTransport(logger.NewSyncTransport(fc)), logger.WithLevel(logger.LevelTrace))
ctx := logger.FCScope(context.Background())
log.DebugContext(ctx, "step 1") // buffered
log.DebugContext(ctx, "step 2") // buffered
log.ErrorContext(ctx, "failed") // flushes step 1 + step 2 + this
}
// Compiled path-DSL redaction masks secrets before any sink sees them.
func ExampleNewPathRedactor() {
pr := logger.NewPathRedactor(logger.Mask, "[SECRET]",
"*.password", "req.headers.authorization")
log := logger.New(
logger.WithProcessors(pr),
logger.WithSink(logger.NewWriterSink(os.Stdout, logger.NewJSONEncoder(), logger.LevelInfo)),
)
log.Info("login", logger.String("user.password", "hunter2")) // → [SECRET]
}
// Spans give scoped context + a causal tree from a flat log stream.
func ExampleLogger_StartSpan() {
log := logger.New()
ctx, span := log.StartSpan(context.Background(), "handle_order",
logger.String("order_id", "o-42"))
defer span.End() // emits span.end with duration + ok
log.InfoContext(ctx, "charging card") // inherits span_id/order_id
}
// Message templates keep a stable key, render text, and emit structured
// fields — all from one call.
func ExampleLogger_Infot() {
log := logger.New()
log.Infot("processed {count} files for {user}", 12, "ada")
// msg="processed 12 files for ada", msg_template kept, count/user structured
}
// The slog bridge: the whole slog ecosystem composes on top.
func ExampleLogger_NewSlog() {
log := logger.New()
sl := log.NewSlog()
sl.Info("via slog", "key", "value")
}