-
Notifications
You must be signed in to change notification settings - Fork 0
/
gorm.go
222 lines (200 loc) · 6.34 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package giu
import (
"context"
"errors"
"fmt"
"time"
"go.uber.org/zap"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/gorm/utils"
)
type GormConnectionParams struct {
Driver string
Host string
Port uint
User string
Password string
Database string
}
type GormConfigParams struct {
*gorm.Config
LogLevel string
}
var _defaultGormParams = GormConnectionParams{
Driver: "mysql",
Host: "localhost",
Port: 3306,
User: "root",
Password: "root",
Database: "test",
}
const (
GORM_DRIVER_MYSQL = "mysql"
GORM_DRIVER_PG = "postgres"
GORM_DRIVER_PG_SHORTEN = "pg"
GORM_DRIVER_SQLITE = "sqlite"
GORM_DRIVER_SQLSERVER = "sqlserver"
)
func NewGorm(params GormConnectionParams, configParams ...*GormConfigParams) (*gorm.DB, error) {
config := &gorm.Config{}
if len(configParams) > 0 && configParams[0] != nil {
param := configParams[0]
if param.Config != nil {
config = configParams[0].Config
}
if param.LogLevel != "" && param.Logger != nil {
level := convertGormLogLevel(configParams[0].LogLevel)
config.Logger = param.Logger.LogMode(level)
}
}
switch params.Driver {
case GORM_DRIVER_MYSQL:
return gorm.Open(NewGormMysql(params), config)
case GORM_DRIVER_PG, GORM_DRIVER_PG_SHORTEN:
return gorm.Open(NewGormPostgres(params), config)
case GORM_DRIVER_SQLITE:
return gorm.Open(NewGormSQLite(params), config)
case GORM_DRIVER_SQLSERVER:
return gorm.Open(NewGormSQLServer(params), config)
default:
return nil, fmt.Errorf("unsupported gorm driver: %s", params.Driver)
}
}
func NewGormWithLogger(params GormConnectionParams, zl *zap.Logger, configParams ...*GormConfigParams) (*gorm.DB, error) {
config := &gorm.Config{}
var logLevel string
if len(configParams) > 0 && configParams[0] != nil {
param := configParams[0]
if param.Config != nil {
config = param.Config
}
if param.LogLevel != "" {
logLevel = param.LogLevel
} else {
logLevel = LOG_LEVEL_ERROR
}
}
var gormLogger logger.Interface
if zl != nil {
gormLogger = NewZapGormLogger(zl.With(zap.String("Database", params.Database)), logLevel)
} else {
return nil, ERR_LOGGER_NOT_INIT
}
config.Logger = gormLogger
return NewGorm(params, &GormConfigParams{Config: config, LogLevel: logLevel})
}
func DefaultGorm() (*gorm.DB, error) {
return NewGorm(_defaultGormParams)
}
func NewGormMysql(params GormConnectionParams) gorm.Dialector {
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", params.User, params.Password, params.Host, params.Port, params.Database)
return mysql.Open(dsn)
}
func NewGormPostgres(params GormConnectionParams) gorm.Dialector {
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable", params.Host, params.User, params.Password, params.Database, params.Port)
return postgres.Open(dsn)
}
func NewGormSQLServer(params GormConnectionParams) gorm.Dialector {
dsn := fmt.Sprintf("sqlserver://%s:%s@%s:%d?database=%s", params.User, params.Password, params.Host, params.Port, params.Database)
return mysql.Open(dsn)
}
func NewGormSQLite(params GormConnectionParams) gorm.Dialector {
dsn := fmt.Sprintf("%s.db", params.Database)
return mysql.Open(dsn)
}
type ZapGormLogger struct {
logger *zap.Logger
logLevel logger.LogLevel
SlowThreshold time.Duration
SkipCallerLookup bool
IgnoreRecordNotFoundError bool
TraceWarnStr string
TraceErrStr string
TraceStr string
}
func NewZapGormLogger(zl *zap.Logger, logLevel string) *ZapGormLogger {
gLevel := convertGormLogLevel(logLevel)
return &ZapGormLogger{
logger: zl,
logLevel: gLevel,
SlowThreshold: 200 * time.Millisecond,
SkipCallerLookup: true,
IgnoreRecordNotFoundError: true,
TraceWarnStr: "[gorm: warning]",
TraceErrStr: "[gorm: error]",
TraceStr: "[gorm: info]",
}
}
func convertGormLogLevel(level string) logger.LogLevel {
switch level {
case LOG_LEVEL_DEBUG:
return logger.Info
case LOG_LEVEL_INFO:
return logger.Info
case LOG_LEVEL_WARN:
return logger.Warn
case LOG_LEVEL_ERROR:
return logger.Error
case LOG_LEVEL_DPANIC:
return logger.Error
case LOG_LEVEL_PANIC:
return logger.Error
case LOG_LEVEL_FATAL:
return logger.Error
default:
return logger.Silent
}
}
func (z *ZapGormLogger) LogMode(level logger.LogLevel) logger.Interface {
newLogger := *z
newLogger.logLevel = level
return &newLogger
}
func (z *ZapGormLogger) Info(ctx context.Context, msg string, data ...interface{}) {
if z.logLevel >= logger.Info {
z.logger.Sugar().Infof(msg, data...)
}
}
func (z *ZapGormLogger) Warn(ctx context.Context, msg string, data ...interface{}) {
if z.logLevel >= logger.Warn {
z.logger.Sugar().Warnf(msg, data...)
}
}
func (z *ZapGormLogger) Error(ctx context.Context, msg string, data ...interface{}) {
if z.logLevel >= logger.Error {
z.logger.Sugar().Errorf(msg, data...)
}
}
func (l *ZapGormLogger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
if l.logLevel <= logger.Silent {
return
}
elapsed := time.Since(begin)
switch {
case err != nil && l.logLevel >= logger.Error && (!errors.Is(err, logger.ErrRecordNotFound) || !l.IgnoreRecordNotFoundError):
sql, rows := fc()
if rows == -1 {
l.logger.Sugar().Errorf(l.TraceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, "-", sql)
} else {
l.logger.Sugar().Errorf(l.TraceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, rows, sql)
}
case elapsed > l.SlowThreshold && l.SlowThreshold != 0 && l.logLevel >= logger.Warn:
sql, rows := fc()
slowLog := fmt.Sprintf("SLOW SQL >= %v", l.SlowThreshold)
if rows == -1 {
l.logger.Sugar().Warn(l.TraceWarnStr, utils.FileWithLineNum(), slowLog, float64(elapsed.Nanoseconds())/1e6, "-", sql)
} else {
l.logger.Sugar().Warn(l.TraceWarnStr, utils.FileWithLineNum(), slowLog, float64(elapsed.Nanoseconds())/1e6, rows, sql)
}
case l.logLevel == logger.Info:
sql, rows := fc()
if rows == -1 {
l.logger.Sugar().Infof(l.TraceStr, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, "-", sql)
} else {
l.logger.Sugar().Infof(l.TraceStr, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, rows, sql)
}
}
}