-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_slog_test.go
82 lines (66 loc) · 1.86 KB
/
example_slog_test.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
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
package gopii_test
import (
"log/slog"
"os"
"github.com/blizzy78/gopii"
)
func Example_slog() {
// define string part types
const (
typePlain = gopii.PartType(iota + 1)
typeLogin
typePassword
)
// define output levels
const (
levelInternal = gopii.Level(iota + 1)
levelDevLog
levelProdLog
levelWorld
)
// define how to obscure each part type at each output level -
// higher levels are obscured more restrictively
obscure := map[gopii.PartType]map[gopii.Level]gopii.ObscureFunc{
// plain text is never obscured
typePlain: {levelWorld: gopii.Plain()},
typeLogin: {
levelInternal: gopii.Plain(),
levelDevLog: gopii.KeepFirstLast(2, 2),
levelProdLog: gopii.KeepFirst(1),
},
typePassword: {
levelInternal: gopii.Plain(),
levelDevLog: gopii.KeepFirst(3),
},
}
// create a new string with parts of different types
str := gopii.NewString(obscure,
"JohnDoe", typeLogin,
":", typePlain,
"secret", typePassword,
)
// create a ReplaceAttr function to obscure strings
replaceAttr := gopii.SlogReplaceAttr(levelDevLog)
// create a handler that writes to stdout
handler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
// use replaceAttr to obscure strings -
// note: please ignore removeExtraAttrs(), this is just for the example
ReplaceAttr: removeExtraAttrs(replaceAttr),
})
// create a new logger
logger := slog.New(handler)
// log the string
logger.Info("", slog.Any("str", str))
// Output:
// str=Jo***oe:sec***
}
// removeExtraAttrs removes attributes that are not needed for the example.
func removeExtraAttrs(next func(groups []string, attr slog.Attr) slog.Attr) func(
groups []string, attr slog.Attr) slog.Attr {
return func(groups []string, attr slog.Attr) slog.Attr {
if attr.Key == "time" || attr.Key == "level" || attr.Key == "msg" {
return slog.Attr{}
}
return next(groups, attr)
}
}