Skip to content

Commit cf53ea9

Browse files
author
xufeng
committed
Fix:BatchNewWithField bug,and supply uni-test code
1 parent fff65d5 commit cf53ea9

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

log/logger/http_logger.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ func splitMsg(msg []interface{}) (withFields []*withField, newMsg []interface{})
132132
switch v.(type) {
133133
case *withField:
134134
withFields = append(withFields, v.(*withField))
135+
case []*withField:
136+
// 如果是通过batchNewWithFields,需要做如下处理
137+
tempWithFieldsList := v.([]*withField)
138+
if len(tempWithFieldsList) != 0 {
139+
for _, tempWithField := range tempWithFieldsList {
140+
withFields = append(withFields, tempWithField)
141+
}
142+
}
135143
default:
136144
newMsg = append(newMsg, v)
137145
}

log/logger/provider_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,33 @@ func TestProvider(t *testing.T) {
114114
}
115115

116116
}
117+
118+
func TestNewWithField(t *testing.T) {
119+
// 测试NewWithField && BatchNewWithField方法
120+
conf := config.LogConfig{
121+
Handler: "file",
122+
Level: "info",
123+
Dir: "../../",
124+
}
125+
126+
defer func() {
127+
if e := recover(); e != nil {
128+
t.Error("test NewWithField panic")
129+
}
130+
}()
131+
err := Pr.Register("logger", conf, true)
132+
if err != nil {
133+
t.Error(err)
134+
return
135+
}
136+
137+
Info(nil, "===TestNewWithField", NewWithField("data", "snow"))
138+
139+
logInfo := map[string]interface{}{
140+
"url": "testUrl",
141+
"params": "snow",
142+
"num": 100,
143+
}
144+
145+
Info(nil, "===TestBatchNewWithField", BatchNewWithField(logInfo))
146+
}

0 commit comments

Comments
 (0)