/
gorm_logger.go
50 lines (41 loc) · 1.28 KB
/
gorm_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
41
42
43
44
45
46
47
48
49
50
package log
import (
"context"
"errors"
"strings"
"time"
"github.com/spf13/viper"
"gorm.io/gorm"
logger2 "gorm.io/gorm/logger"
)
// // Interface logger interface
//type Interface interface {
// LogMode(LogLevel) Interface
// Info(context.Context, string, ...interface{})
// Warn(context.Context, string, ...interface{})
// Error(context.Context, string, ...interface{})
// Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error)
//}
type GormLogger struct{}
func (GormLogger) LogMode(_ logger2.LogLevel) logger2.Interface {
// do nothing
// we use our own log level system.
return GormLogger{}
}
func (GormLogger) Info(_ context.Context, msg string, param ...interface{}) {
Infof(msg, param...)
}
func (GormLogger) Warn(_ context.Context, msg string, param ...interface{}) {
Warningf(msg, param...)
}
func (GormLogger) Error(_ context.Context, msg string, param ...interface{}) {
Errorf(msg, param...)
}
func (GormLogger) Trace(_ context.Context, begin time.Time, fc func() (string, int64), err error) {
sql, rows := fc()
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
Errorf("%v, SQL: %s, rows: %v", err, sql, rows)
} else if viper.GetBool("debug") && !strings.Contains(sql, "INSERT INTO \"logs\"") {
Debugf("SQL: %s, rows: %v", sql, rows)
}
}