forked from onexstack/onex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgorm.go
112 lines (96 loc) · 3.26 KB
/
gorm.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2022 Lingfei Kong <[email protected]>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file. The original repo for
// this file is https://github.com/superproj/onex.
//
package log
import (
"context"
"fmt"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
gormlogger "gorm.io/gorm/logger"
)
var (
infoStr = "%s[info] "
warnStr = "%s[warn] "
errStr = "%s[error] "
traceStr = "[%s][%.3fms] [rows:%v] %s"
traceWarnStr = "%s %s[%.3fms] [rows:%v] %s"
traceErrStr = "%s %s[%.3fms] [rows:%v] %s"
slowThreshold = 200 * time.Millisecond
)
var levelM = map[string]gormlogger.LogLevel{
"panic": gormlogger.Silent,
"error": gormlogger.Error,
"warn": gormlogger.Warn,
"info": gormlogger.Info,
}
func (l *zapLogger) LogMode(level gormlogger.LogLevel) gormlogger.Interface {
opts := *l.opts
switch {
case level <= gormlogger.Silent:
opts.Level = "panic"
case level <= gormlogger.Error:
opts.Level = "error"
case level <= gormlogger.Warn:
opts.Level = "warn"
case level <= gormlogger.Info:
opts.Level = "info"
default:
}
return NewLogger(&opts)
}
func (l *zapLogger) Info(ctx context.Context, msg string, keyvals ...any) {
l.z.Sugar().Infof(infoStr+msg, append([]any{fileWithLineNum()}, keyvals...)...)
}
func (l *zapLogger) Warn(ctx context.Context, msg string, keyvals ...any) {
l.z.Sugar().Warnf(warnStr+msg, append([]any{fileWithLineNum()}, keyvals...)...)
}
func (l *zapLogger) Error(ctx context.Context, msg string, keyvals ...any) {
l.z.Sugar().Errorf(errStr+msg, append([]any{fileWithLineNum()}, keyvals...)...)
}
func (l *zapLogger) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) {
if levelM[l.opts.Level] <= gormlogger.Silent {
return
}
elapsed := time.Since(begin)
switch {
case err != nil && levelM[l.opts.Level] >= gormlogger.Error:
sql, rows := fc()
if rows == -1 {
l.z.Sugar().Errorf(traceErrStr, fileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, "-", sql)
} else {
l.z.Sugar().Errorf(traceErrStr, fileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, rows, sql)
}
case elapsed > slowThreshold && slowThreshold != 0 && levelM[l.opts.Level] >= gormlogger.Warn:
sql, rows := fc()
slowLog := fmt.Sprintf("SLOW SQL >= %v", slowThreshold)
if rows == -1 {
l.z.Sugar().Warnf(traceWarnStr, fileWithLineNum(), slowLog, float64(elapsed.Nanoseconds())/1e6, "-", sql)
} else {
l.z.Sugar().Warnf(traceWarnStr, fileWithLineNum(), slowLog, float64(elapsed.Nanoseconds())/1e6, rows, sql)
}
case levelM[l.opts.Level] >= gormlogger.Info:
sql, rows := fc()
if rows == -1 {
l.z.Sugar().Infof(traceStr, fileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, "-", sql)
} else {
l.z.Sugar().Infof(traceStr, fileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, rows, sql)
}
}
}
func fileWithLineNum() string {
for i := 4; i < 15; i++ {
_, file, line, ok := runtime.Caller(i)
// if ok && (!strings.HasPrefix(file, gormSourceDir) || strings.HasSuffix(file, "_test.go")) {
if ok && !strings.HasSuffix(file, "_test.go") {
dir, f := filepath.Split(file)
return filepath.Join(filepath.Base(dir), f) + ":" + strconv.FormatInt(int64(line), 10)
}
}
return ""
}