增加gorm日志实现 - 待调试

This commit is contained in:
白茶清欢 2021-03-01 22:44:38 +08:00
parent 84bf18c576
commit e4ad2d2b98
4 changed files with 161 additions and 1 deletions

2
go.mod
View File

@ -11,4 +11,6 @@ require (
github.com/spaolacci/murmur3 v1.1.0
github.com/stretchr/testify v1.7.0
go.uber.org/zap v1.16.0
gorm.io/driver/mysql v1.0.4
gorm.io/gorm v1.20.12
)

10
go.sum
View File

@ -23,6 +23,8 @@ github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GO
github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg=
github.com/go-redis/redis/v8 v8.6.0 h1:swqbqOrxaPztsj2Hf1p94M3YAgl7hYEpcw21z299hh8=
github.com/go-redis/redis/v8 v8.6.0/go.mod h1:DQ9q4Rk2HtwkrwVrdgmphoOQDMfpvcd/nHEwRsicg8s=
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
@ -40,6 +42,10 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.1 h1:g39TucaRWyV3dwDO++eEc6qf8TVIQ/Da48WmqjZ3i7E=
github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
@ -162,4 +168,8 @@ gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/mysql v1.0.4 h1:TATTzt+kR+IV0+h3iUB3dHUe8omCvQ0rOkmfCsUBohk=
gorm.io/driver/mysql v1.0.4/go.mod h1:MEgp8tk2n60cSBCq5iTcPDw3ns8Gs+zOva9EUhkknTs=
gorm.io/gorm v1.20.12 h1:ebZ5KrSHzet+sqOCVdH9mTjW91L298nX3v5lVxAzSUY=
gorm.io/gorm v1.20.12/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=

148
logger/wrapper/gorm_v2.go Normal file
View File

@ -0,0 +1,148 @@
// Package wrapper...
//
// Description : gorm v2 版本接口实现
//
// Author : go_developer@163.com<张德满>
//
// Date : 2021-03-01 9:52 下午
package wrapper
import (
"context"
"fmt"
"time"
"gorm.io/gorm"
"go.uber.org/zap/zapcore"
"go.uber.org/zap"
logger2 "github.com/go-developer/gopkg/logger"
"gorm.io/gorm/logger"
)
// NewGormV2 获取日志实例
//
// Author : go_developer@163.com<张德满>
//
// Date : 9:56 下午 2021/3/1
func NewGormV2(loggerLevel zapcore.Level, consoleOutput bool, encoder zapcore.Encoder, splitConfig *logger2.RotateLogConfig, traceIDField string) (logger.Interface, error) {
logInstance, err := logger2.NewLogger(loggerLevel, consoleOutput, encoder, splitConfig)
if nil != err {
return nil, err
}
return &Gorm{
instance: logInstance,
}, nil
}
// Gorm v2 版本库日志实现
//
// Author : go_developer@163.com<张德满>
//
// Date : 9:55 下午 2021/3/1
type Gorm struct {
instance *zap.Logger // 日志实例
traceIDField string // 串联请求上下文的的ID
flag string // 数据库标识
}
// LogMode ...
//
// Author : go_developer@163.com<张德满>
//
// Date : 10:08 下午 2021/3/1
func (g *Gorm) LogMode(level logger.LogLevel) logger.Interface {
return g
}
// Info 日志
//
// Author : go_developer@163.com<张德满>
//
// Date : 10:18 下午 2021/3/1
func (g *Gorm) Info(ctx context.Context, s string, i ...interface{}) {
g.instance.Info(
"Info日志",
zap.String(g.traceIDField, g.getTraceID(ctx)),
zap.String("db_flag", g.flag),
zap.String("message", fmt.Sprintf(s, i...)),
)
}
// Warn ...
//
// Author : go_developer@163.com<张德满>
//
// Date : 10:16 下午 2021/3/1
func (g *Gorm) Warn(ctx context.Context, s string, i ...interface{}) {
g.instance.Warn(
"SQL执行产生Warning",
zap.String(g.traceIDField, g.getTraceID(ctx)),
zap.String("db_flag", g.flag),
zap.String("message", fmt.Sprintf(s, i...)),
)
}
// Error 日志
//
// Author : go_developer@163.com<张德满>
//
// Date : 10:18 下午 2021/3/1
func (g *Gorm) Error(ctx context.Context, s string, i ...interface{}) {
g.instance.Warn(
"SQL执行产生Error",
zap.String(g.traceIDField, g.getTraceID(ctx)),
zap.String("db_flag", g.flag),
zap.String("message", fmt.Sprintf(s, i...)),
)
}
// Trace Trace 记录
//
// Author : go_developer@163.com<张德满>
//
// Date : 10:19 下午 2021/3/1
func (g *Gorm) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
start := begin.UnixNano()
end := time.Now().UnixNano()
sql := ""
affectRows := int64(0)
if nil != fc {
sql, affectRows = fc()
}
g.instance.Info(
"SQL执行记录",
zap.String(g.traceIDField, g.getTraceID(ctx)),
zap.String("db_flag", g.flag),
zap.Int64("begin_time", start),
zap.Int64("finish_time", end),
zap.String("used_time", fmt.Sprintf("%fms", float64(end-start)/1e6)),
zap.String("sql", sql),
zap.Int64("affect_rows", affectRows),
zap.Error(err),
)
return
}
// getTraceID 获取traceID
//
// Author : go_developer@163.com<张德满>
//
// Date : 10:11 下午 2021/3/1
func (g *Gorm) getTraceID(ctx context.Context) string {
return fmt.Sprintf("%v", ctx.Value(g.traceIDField))
}
// GetGormSQL 获取tracefn
//
// Author : go_developer@163.com<张德满>
//
// Date : 10:38 下午 2021/3/1
func GetGormSQL(dbClient *gorm.DB) func() (string, int64) {
return func() (string, int64) {
return dbClient.Dialector.Explain(dbClient.Statement.SQL.String(), dbClient.Statement.Vars...), dbClient.RowsAffected
}
}

View File

@ -23,7 +23,7 @@ import (
// Date : 10:22 下午 2021/2/27
func TestCommandProxy(t *testing.T) {
instance, err := NewClient(map[string]Options{
"test_redis": Options{
"test_redis": {
Conf: &redisInstance.Options{
Addr: "127.0.0.1:6379",
},