-
Notifications
You must be signed in to change notification settings - Fork 5
/
gzap_test.go
80 lines (71 loc) · 1.74 KB
/
gzap_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
package gzap
import (
"testing"
graylog "github.com/Devatoria/go-graylog"
)
func TestInitLogger(t *testing.T) {
type args struct {
graylogAppName string
isTestEnv bool
graylogHost string
graylogHandlerType graylog.Transport
graylogLogEnvName string
jsonformatter bool
}
tests := []struct {
name string
args args
wantErr bool
err string
}{
{
"InitLogger should return a noop logger when running a test",
args{
isTestEnv: true,
},
false,
"",
},
{
"InitLogger should return a dev logger when no GRAYLOG_HOST is set",
args{},
false,
"",
},
{
"InitLogger should return a json logger when ENABLE_DATADOG_JSON_FORMATTER is set",
args{
jsonformatter: true,
},
false,
"",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Instaniate new MockEnvConfig.
cfg := MockEnvConfig{}
cfg.On("enableJSONFormatter").Return(tt.args.jsonformatter)
cfg.On("getGraylogAppName").Return(tt.args.graylogAppName)
cfg.On("getIsTestEnv").Return(tt.args.isTestEnv)
cfg.On("getGraylogHost").Return(tt.args.graylogHost)
cfg.On("getGraylogHandlerType").Return(tt.args.graylogHandlerType)
cfg.On("getGraylogLogEnvName").Return(tt.args.graylogLogEnvName)
cfg.On("useColoredConsolelogs").Return(true)
err := initLogger(&cfg, false)
if tt.wantErr && err == nil {
t.Errorf("initLogger() expected error = \"%v\"; got \"nil\"", tt.err)
}
if err != nil && err.Error() != tt.err {
t.Errorf("initLogger() expected error = \"%v\"; got \"%v\"", tt.err, err.Error())
}
})
}
}
func ExampleInitLogger() {
if err := InitLogger(); err != nil {
panic(err)
}
defer Logger.Sync()
Logger.Info("this is a test info log")
}