-
Notifications
You must be signed in to change notification settings - Fork 1
/
rzap_test.go
110 lines (93 loc) · 2.43 KB
/
rzap_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
99
100
101
102
103
104
105
106
107
108
109
110
package rzap
import (
"encoding/json"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
"io/ioutil"
"os"
"testing"
)
type line struct {
Status int `json:"status"`
Level string `json:"level"`
TS string `json:"ts"`
Message string `json:"msg"`
}
func expectRemoveFile(filePath string, t *testing.T) {
if _, err := os.Stat(filePath); err == nil {
if err := os.Remove(filePath); err != nil {
t.Errorf("Remove file: %s failed", filePath)
}
}
}
func expectExistFile(filePath string, t *testing.T) {
if _, err := os.Stat(filePath); err != nil {
t.Errorf("File: %s not exist", filePath)
}
}
func expectReadFile(filePath string, t *testing.T) *line {
b, err := ioutil.ReadFile(filePath)
if err != nil {
t.Errorf("Read file: %s failed", filePath)
return nil
}
l := &line{}
if err := json.Unmarshal(b, l); err != nil {
t.Errorf("Unmarshal line: %s failed", string(b))
return nil
}
return l
}
func TestNewLogger(t *testing.T) {
logFile := "/tmp/rzap.log"
expectRemoveFile(logFile, t)
core := NewCore(&lumberjack.Logger{
Filename: "/tmp/rzap.log",
}, zap.InfoLevel)
NewGlobalLogger([]zapcore.Core{core})
zap.L().Info("info message", zap.Int("status", 0))
expectExistFile(logFile, t)
l := expectReadFile(logFile, t)
if l == nil {
return
}
if l.Status != 0 {
t.Error("Log 'status' error")
}
if l.Level != "INFO" {
t.Error("Log 'level' error")
}
if l.Message != "info message" {
t.Error("Log 'msg' error")
}
}
func TestNewTeeLogger(t *testing.T) {
infoLogFile := "/tmp/rzap_info.log"
errorLogFile := "/tmp/rzap_error.log"
expectRemoveFile(infoLogFile, t)
expectRemoveFile(errorLogFile, t)
infoCore := NewCore(&lumberjack.Logger{
Filename: infoLogFile,
}, zap.LevelEnablerFunc(func(level zapcore.Level) bool {
return level <= zap.InfoLevel
}))
errorCore := NewCore(&lumberjack.Logger{
Filename: errorLogFile,
}, zap.LevelEnablerFunc(func(level zapcore.Level) bool {
return level > zap.InfoLevel
}))
NewGlobalLogger([]zapcore.Core{infoCore, errorCore})
zap.L().Info("info message", zap.Int("status", 0))
zap.L().Error("error message", zap.Int("status", 1))
expectExistFile(infoLogFile, t)
expectExistFile(errorLogFile, t)
infoLine := expectReadFile(infoLogFile, t)
errorLine := expectReadFile(errorLogFile, t)
if infoLine == nil || errorLine == nil {
return
}
if infoLine.Level != "INFO" || errorLine.Level != "ERROR" {
t.Error("Log 'level' error")
}
}