-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging_test.go
91 lines (74 loc) · 2.46 KB
/
logging_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
package logging
import (
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"go.uber.org/zap/zapcore"
"os"
"testing"
)
func init() {
}
// test init by default
func TestInitZapLoggerFromViper1(t *testing.T) {
TestInitZapLoggerFromViper2(t)
InitZapLoggerFromViper(viper.GetViper())
InitZapLoggerFromViper(viper.GetViper())
InitZapLoggerFromViper(viper.GetViper())
InitZapLoggerFromViper(viper.GetViper())
assert.NotNil(t, logger)
logger.Sugar().Info("TestInitZapLoggerFromViper")
logger.Sugar().Info("TestInitZapLoggerFromViper")
}
// test init by config.yaml
func TestInitZapLoggerFromViper2(t *testing.T) {
gopath, b := os.LookupEnv("GOPATH")
assert.Truef(t, b, "no GOPATH ENV")
viper.SetConfigFile(gopath + "/src/github.com/izern/logging-go/config.yaml")
err := viper.ReadInConfig()
get := viper.GetViper().Get("logging.output.console")
assert.Nil(t, get)
set := viper.GetViper().IsSet("logging.output.console")
assert.False(t, set, "")
assert.NoError(t, err)
InitZapLoggerFromViper(viper.GetViper())
assert.NotNil(t, logger)
logger.Sugar().Info("TestInitZapLoggerFromViper")
}
// test by
func TestGetLevel(t *testing.T) {
TestInitZapLoggerFromViper2(t)
level := GetLevel("test")
assert.Equal(t, level, zapcore.InfoLevel, level.String())
level = GetLevel("module1.child1")
assert.Equal(t, level, zapcore.DebugLevel, level.String())
level = GetLevel("module1.child1.child")
assert.Equal(t, level, zapcore.DebugLevel, level.String())
level = GetLevel("module")
assert.Equal(t, level, zapcore.InfoLevel, level.String())
level = GetLevel("module2")
assert.Equal(t, level, zapcore.ErrorLevel, level.String())
}
func TestGetLogger(t *testing.T) {
TestInitZapLoggerFromViper2(t)
log := GetLogger("test")
assert.NotNil(t, log)
assert.True(t, log.Core().Enabled(zapcore.InfoLevel))
assert.False(t, log.Core().Enabled(zapcore.DebugLevel))
log.Info("INFO must be show")
log.Debug("DEBUG must be hide")
log = GetLogger("module1.child1")
assert.NotNil(t, log)
assert.True(t, log.Core().Enabled(zapcore.DebugLevel))
log.Debug("DEBUG must be show")
log = GetLogger("module1.child1.child")
assert.NotNil(t, log)
assert.True(t, log.Core().Enabled(zapcore.DebugLevel))
log = GetLogger("module2")
assert.NotNil(t, log)
assert.True(t, log.Core().Enabled(zapcore.ErrorLevel))
assert.False(t, log.Core().Enabled(zapcore.WarnLevel))
log2 := GetLogger("module2")
assert.True(t, log == log2)
log3 := GetLogger("module3")
assert.False(t, log == log3)
}