-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
zapgorm.go
37 lines (31 loc) · 890 Bytes
/
zapgorm.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 zapgorm // import "moul.io/zapgorm"
import (
"time"
"go.uber.org/zap"
)
type Logger struct {
zap *zap.Logger
}
func New(logger *zap.Logger) Logger {
return Logger{zap: logger}
}
func (l Logger) Print(values ...interface{}) {
if len(values) < 2 {
return
}
switch values[0] {
case "sql":
l.zap.Debug("gorm.debug.sql",
zap.String("query", values[3].(string)),
zap.Any("values", values[4]),
zap.Float64("duration in ms", float64(values[2].(time.Duration))/float64(time.Millisecond)),
zap.Int64("affected-rows", values[5].(int64)),
zap.String("source", values[1].(string)), // if AddCallerSkip(6) is well defined, we can safely remove this field
)
default:
l.zap.Debug("gorm.debug.other",
zap.Any("values", values[2:]),
zap.String("source", values[1].(string)), // if AddCallerSkip(6) is well defined, we can safely remove this field
)
}
}