-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel_test.go
98 lines (82 loc) · 1.9 KB
/
level_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package L_test
import (
"bytes"
"fmt"
"github.com/scott-cotton/L"
)
type LogLevel int
const (
Panic LogLevel = 1 + iota
Fatal
Warn
Notice
Info
Debug
Trace
)
func (lvl LogLevel) String() string {
return map[LogLevel]string{
Panic: "panic",
Fatal: "fatal",
Warn: "warn",
Notice: "notice",
Info: "info",
Debug: "debug",
Trace: "trace",
}[lvl]
}
// set output to a global buffer for testing.
var lvlW = bytes.NewBuffer(nil)
func tagLevel(cfg *L.Config, o *L.Obj) *L.Obj {
v := cfg.Labels["Lop"]
if v == 0 {
return o
}
return o.Field("Lop", LogLevel(v).String())
}
func filterLevel(cfg *L.Config, o *L.Obj) *L.Obj {
v := cfg.Labels["Lop"]
if v == 0 {
return o
}
if LogLevel(v) <= Level {
return o
}
return nil
}
func LConfig() *L.Config {
res := &L.Config{
Labels: map[string]int{"Lop": int(Level)},
W: lvlW,
F: L.JSONFmter(),
E: L.EPanic,
Pre: []L.Middleware{filterLevel},
Post: []L.Middleware{L.Pkg(), tagLevel},
}
return res
}
// global variable can be set at runtime.
// to expose this via the L service,
// one would put the value in a tag.
var Level = Debug
var (
Ldebug = L.New(LConfig())
Ltrace = Ldebug.With("Lop", int(Trace))
Lnotice = Ldebug.With("Lop", int(Notice))
Linfo = Ltrace.With("Lop", int(Info))
Lwarn = Ltrace.With("Lop", int(Warn))
Lfatal = Ltrace.With("Lop", int(Fatal))
Lpanic = Ltrace.With("Lop", int(Panic))
)
func Example_levels() {
Ltrace.Dict().Field("hello-trace", 22).Log()
Ldebug.Dict().Field("hello-debug", 33).Log()
Level = Trace
Ltrace.Dict().Field("hello-trace", 44).Log()
Ldebug.Dict().Field("hello-debug", 55).Log()
fmt.Printf("%s\n", lvlW.String())
// Output:
// {"hello-debug":33,"Lpkg":"github.com/scott-cotton/L_test","Lop":"debug"}
// {"hello-trace":44,"Lpkg":"github.com/scott-cotton/L_test","Lop":"trace"}
// {"hello-debug":55,"Lpkg":"github.com/scott-cotton/L_test","Lop":"debug"}
}