-
Notifications
You must be signed in to change notification settings - Fork 24
/
log.go
50 lines (42 loc) · 951 Bytes
/
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
package gormzap
import (
"fmt"
"time"
"github.com/jinzhu/gorm"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type log struct {
occurredAt time.Time
source string
duration time.Duration
sql string
values []string
other []string
}
func (l *log) toZapFields() []zapcore.Field {
return []zapcore.Field{
zap.Time("occurredAt", l.occurredAt),
zap.String("source", l.source),
zap.Duration("duration", l.duration),
zap.String("sql", l.sql),
zap.Strings("values", l.values),
zap.Strings("other", l.other),
}
}
func createLog(values []interface{}) *log {
ret := &log{}
ret.occurredAt = gorm.NowFunc()
if len(values) > 1 {
var level = values[0]
ret.source = getSource(values)
if level == "sql" {
ret.duration = getDuration(values)
ret.values = getFormattedValues(values)
ret.sql = values[3].(string)
} else {
ret.other = append(ret.other, fmt.Sprint(values[2:]))
}
}
return ret
}