-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
logger.go
66 lines (55 loc) · 1.61 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package dbresolver
import (
"context"
"fmt"
"time"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
type ResolverModeKey string
type ResolverMode string
const resolverModeKey ResolverModeKey = "dbresolver:resolver_mode_key"
const (
ResolverModeSource ResolverMode = "source"
ResolverModeReplica ResolverMode = "replica"
)
type resolverModeLogger struct {
logger.Interface
}
func (l resolverModeLogger) ParamsFilter(ctx context.Context, sql string, params ...interface{}) (string, []interface{}) {
if filter, ok := l.Interface.(gorm.ParamsFilter); ok {
sql, params = filter.ParamsFilter(ctx, sql, params...)
}
return sql, params
}
func (l resolverModeLogger) LogMode(level logger.LogLevel) logger.Interface {
l.Interface = l.Interface.LogMode(level)
return l
}
func (l resolverModeLogger) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) {
var splitFn = func() (sql string, rowsAffected int64) {
sql, rowsAffected = fc()
op := ctx.Value(resolverModeKey)
if op != nil {
sql = fmt.Sprintf("[%s] %s", op, sql)
return
}
// the situation that dbresolver does not handle
// such as transactions, or some resolvers do not enable MarkResolverMode.
return
}
l.Interface.Trace(ctx, begin, splitFn, err)
}
func NewResolverModeLogger(l logger.Interface) logger.Interface {
if _, ok := l.(resolverModeLogger); ok {
return l
}
return resolverModeLogger{
Interface: l,
}
}
func markStmtResolverMode(stmt *gorm.Statement, mode ResolverMode) {
if _, ok := stmt.Logger.(resolverModeLogger); ok {
stmt.Context = context.WithValue(stmt.Context, resolverModeKey, mode)
}
}