-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogger_test.go
More file actions
196 lines (147 loc) · 5.4 KB
/
Copy pathlogger_test.go
File metadata and controls
196 lines (147 loc) · 5.4 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package logger
import (
"context"
"fmt"
"os"
"testing"
"time"
"github.com/google/uuid"
)
func TestLogger(test *testing.T) {
showsystem := "showsystem"
hidesystem := "hidesystem"
{
logConfig := NewConfig(true, false, "")
logConfig.EnableSubSystem(showsystem)
// fmt.Printf("Original log config : %+v\n", logConfig)
ctx := ContextWithLogConfig(context.Background(), logConfig)
Log(ctx, LevelInfo, "First main entry")
Log(ctx, LevelInfo, "First main entry with value : %d", 101)
showCtx := ContextWithLogSubSystem(ctx, showsystem)
Log(showCtx, LevelInfo, "First Sub entry")
hideCtx := ContextWithLogSubSystem(ctx, hidesystem)
Log(hideCtx, LevelInfo, "First Hidden Sub entry. You should not see this!")
// fmt.Printf("Log config after hide : %+v\n", logConfig)
Log(ctx, LevelInfo, "Second main entry")
ctxTrace1 := ContextWithLogTrace(ctx, "trace 1")
Log(ctxTrace1, LevelInfo, "Entry with trace 1")
fmt.Printf("^ should contain \"trace 1\"\n")
ctxTrace2 := ContextWithLogTrace(ctx, "trace 2")
Log(ctxTrace2, LevelInfo, "Entry with trace 2")
fmt.Printf("^ should contain \"trace 2\"\n")
}
}
func TestSubSystem(test *testing.T) {
logConfig := NewConfig(false, false, "")
logConfig.EnableSubSystem("SpyNode")
ctx := ContextWithLogConfig(context.Background(), logConfig)
log := NewLoggerObject(ctx)
spyCtx := ContextWithLogSubSystem(ctx, "SpyNode")
wospyCtx := ContextWithOutLogSubSystem(ctx)
Log(ctx, LevelInfo, "Without Spynode")
Log(spyCtx, LevelInfo, "With Spynode")
Log(wospyCtx, LevelInfo, "Without Spynode")
log.Printf("Print")
}
func TestDisabledSubSystem(test *testing.T) {
logConfig := NewConfig(false, false, "")
ctx := ContextWithLogConfig(context.Background(), logConfig)
spyCtx := ContextWithLogSubSystem(ctx, "SpyNode")
wospyCtx := ContextWithOutLogSubSystem(ctx)
Log(ctx, LevelInfo, "Without Spynode")
Log(spyCtx, LevelInfo, "With Spynode")
Log(wospyCtx, LevelInfo, "Without Spynode")
}
func TestFields(t *testing.T) {
ctx := ContextWithLogger(context.Background(), false, false, "")
s := String("string", "value")
i := Int("integer", 10)
ui := Uint("unsigned int", uint(20))
f := Float32("float32", 1.0)
f64 := Float64("float64", 2.0)
InfoWithFields(ctx, []Field{s, i, ui, f, f64}, "String, Int, Uint, Float32, Float64")
stringWithQuotes := String("with quote", `"should escape quote`)
stringWithBackspace := String("with backspace", "\b should escape backspace")
InfoWithFields(ctx, []Field{stringWithQuotes, stringWithBackspace}, "String, String")
stringWithNewLine := String("with newline", `
should escape newline and tab`)
InfoWithFields(ctx, []Field{stringWithNewLine}, "String")
stringWithBackslash := String("with backslash", `\ should escape backslash`)
InfoWithFields(ctx, []Field{stringWithBackslash}, "String")
hex := Hex("hex", []byte{1, 2, 3})
InfoWithFields(ctx, []Field{hex}, "Hex")
u32s := Uint32s("uint list", []uint32{1, 2, 3})
InfoWithFields(ctx, []Field{u32s}, "Uint32s")
float32s := Float32s("float list", []float32{1.234, 2.948463, 3.1})
InfoWithFields(ctx, []Field{float32s}, "Float32s")
json := struct {
Field1 string `json:"field_1"`
Field2 int `json:"field_2"`
}{
Field1: "value 1",
Field2: 2,
}
jsonField := JSON("json_struct", &json)
InfoWithFields(ctx, []Field{jsonField}, "JSON")
}
func Test_DuplicateFields(t *testing.T) {
ctx := ContextWithLogger(context.Background(), false, false, "")
ctx = ContextWithLogFields(ctx, String("duplicate", "original"))
InfoWithFields(ctx, []Field{String("duplicate", "should not show")}, "Message")
}
func TestWaitWarning(t *testing.T) {
ctx := ContextWithLogger(context.Background(), false, false, "")
waitWarning := NewWaitingWarning(ctx, 500*time.Millisecond, "Print this 4 times")
time.Sleep(2 * time.Second)
waitWarning.Cancel()
}
func BenchmarkContextWithLogTrace(b *testing.B) {
ctx := ContextWithLogConfig(context.Background(), NewConfig(false, false, ""))
for i := 0; i < b.N; i++ {
ContextWithLogTrace(ctx, "trace")
}
}
func BenchmarkContextWithOutLogSubSystem(b *testing.B) {
ctx := ContextWithLogConfig(context.Background(), NewConfig(false, false, ""))
for i := 0; i < b.N; i++ {
ContextWithOutLogSubSystem(ctx)
}
}
func BenchmarkFileNoFields(b *testing.B) {
os.Mkdir("./tmp", 0755)
logFileName := "./tmp/bench_" + uuid.New().String() + ".log"
ctx := ContextWithLogConfig(context.Background(), NewConfig(false, false, logFileName))
for i := 0; i < b.N; i++ {
Info(ctx, "Simple log entry %d", i)
}
os.Remove(logFileName)
}
func BenchmarkFileWithFields(b *testing.B) {
os.Mkdir("./tmp", 0755)
logFileName := "./tmp/bench_" + uuid.New().String() + ".log"
ctx := ContextWithLogConfig(context.Background(), NewConfig(false, false, logFileName))
for i := 0; i < b.N; i++ {
InfoWithFields(ctx, []Field{
String("title", "string value"),
Int("index", i),
Float32("float", 123.556),
}, "Simple log entry with fields")
}
os.Remove(logFileName)
}
func BenchmarkDummyNoFields(b *testing.B) {
ctx := ContextWithLogConfig(context.Background(), NewConfig(false, false, "dummy"))
for i := 0; i < b.N; i++ {
Info(ctx, "Simple log entry %d", i)
}
}
func BenchmarkDummyWithFields(b *testing.B) {
ctx := ContextWithLogConfig(context.Background(), NewConfig(false, false, "dummy"))
for i := 0; i < b.N; i++ {
InfoWithFields(ctx, []Field{
String("title", "string value"),
Int("index", i),
Float32("float", 123.556),
}, "Simple log entry with fields")
}
}