forked from zaffka/zap-to-hclog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.go
37 lines (31 loc) · 806 Bytes
/
convert.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
package wrapper
import (
"fmt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Assumed, we'll get key-value pairs as arguments.
// Code below prevents a panic, if wrong arguments set received.
func convertToZapAny(args ...interface{}) []zapcore.Field {
fields := []zapcore.Field{}
for i := len(args); i > 0; i -= 2 {
left := i - 2
if left < 0 {
left = 0
}
items := args[left:i]
switch l := len(items); l {
case 2:
k, ok := items[0].(string)
if ok {
fields = append(fields, zap.Any(k, items[1]))
} else {
fields = append(fields, zap.Any(fmt.Sprintf("arg%d", i-1), items[1]))
fields = append(fields, zap.Any(fmt.Sprintf("arg%d", left), items[0]))
}
case 1:
fields = append(fields, zap.Any(fmt.Sprintf("arg%d", left), items[0]))
}
}
return fields
}