-
Notifications
You must be signed in to change notification settings - Fork 3
/
log.go
68 lines (57 loc) · 1.22 KB
/
log.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
package gocron
import (
"github.com/zhufuyi/pkg/logger"
"github.com/robfig/cron/v3"
"go.uber.org/zap"
)
type myLog struct {
zapLog *zap.Logger
}
func (l *myLog) Info(msg string, keysAndValues ...interface{}) {
if msg == "wake" { // 忽略wake
return
}
msg = "cron_" + msg
fields := parseKVs(keysAndValues)
if l.zapLog != nil {
l.zapLog.Info(msg, fields...)
} else {
logger.Info(msg, fields...)
}
}
func (l *myLog) Error(err error, msg string, keysAndValues ...interface{}) {
fields := parseKVs(keysAndValues)
fields = append(fields, zap.String("err", err.Error()))
msg = "cron_" + msg
if l.zapLog != nil {
l.zapLog.Error(msg, fields...)
} else {
logger.Error(msg, fields...)
}
}
func parseKVs(kvs interface{}) []zap.Field {
var fields []zap.Field
infos, ok := kvs.([]interface{})
if !ok {
return fields
}
l := len(infos)
if l%2 == 1 {
return fields
}
for i := 0; i < l; i += 2 {
key := infos[i].(string) //nolint
value := infos[i+1]
// 把id替换为任务名称
if key == "entry" {
if id, ok := value.(cron.EntryID); ok {
key = "task"
if v, isExist := idName.Load(id); isExist {
value = v
}
}
}
fields = append(fields, zap.Any(key, value))
}
return fields
}