-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
40 lines (31 loc) · 942 Bytes
/
logger.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
package gormx
import (
"context"
"time"
"github.com/boxgo/box/pkg/logger"
gormlog "gorm.io/gorm/logger"
)
type (
Logger struct{}
)
func (l *Logger) LogMode(gormlog.LogLevel) gormlog.Interface {
return l
}
func (*Logger) Info(ctx context.Context, tmpl string, args ...interface{}) {
logger.Trace(ctx).Infof(tmpl, args...)
}
func (*Logger) Warn(ctx context.Context, tmpl string, args ...interface{}) {
logger.Trace(ctx).Warnf(tmpl, args...)
}
func (*Logger) Error(ctx context.Context, tmpl string, args ...interface{}) {
logger.Trace(ctx).Errorf(tmpl, args...)
}
func (*Logger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
sql, n := fc()
elapsed := time.Since(begin)
if err != nil {
logger.Trace(ctx).Warnw("GormExecuteError", "sql", sql, "elapsed", elapsed, "error", err)
} else {
logger.Trace(ctx).Debugw("GormExecuteSuccess", "sql", sql, "elapsed", elapsed, "effect", n)
}
}