-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.go
62 lines (54 loc) · 1.67 KB
/
sql.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
package logger
import (
"context"
"fmt"
"github.com/stubborn-gaga-0805/prepare2go/pkg/helpers"
"gorm.io/gorm/logger"
"log"
"os"
"time"
)
type GormLogger struct {
ctx context.Context `json:"ctx,omitempty"`
logger.Interface `json:"logger.Interface,omitempty"`
}
func NewGormLogger(ctx context.Context) *GormLogger {
return &GormLogger{
ctx,
logger.New(
log.New(os.Stdout, "\n", log.LstdFlags),
logger.Config{
LogLevel: logger.Info,
SlowThreshold: 200 * time.Millisecond,
IgnoreRecordNotFoundError: false,
Colorful: true,
ParameterizedQueries: false,
},
),
}
}
func (l *GormLogger) LogMode(level logger.LogLevel) logger.Interface {
gLogger := *l
gLogger.Interface = l.Interface.LogMode(level)
return &gLogger
}
func (l *GormLogger) Info(ctx context.Context, msg string, data ...interface{}) {
msg = fmt.Sprintf("[%s] %s", helpers.GetRequestIdFromContext(ctx), msg)
l.Interface.Info(ctx, msg, data...)
}
func (l *GormLogger) Warn(ctx context.Context, msg string, data ...interface{}) {
msg = fmt.Sprintf("[%s] %s", helpers.GetRequestIdFromContext(ctx), msg)
l.Interface.Warn(ctx, msg, data...)
}
func (l *GormLogger) Error(ctx context.Context, msg string, data ...interface{}) {
msg = fmt.Sprintf("[%s] %s", helpers.GetRequestIdFromContext(ctx), msg)
l.Interface.Error(ctx, msg, data...)
}
func (l *GormLogger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) { /**/
sql, rows := fc()
nfc := func() (string, int64) {
msg := fmt.Sprintf("[%s] %s", helpers.GetRequestIdFromContext(ctx), sql)
return msg, rows
}
l.Interface.Trace(ctx, begin, nfc, err)
}