gormLogger
This commit is contained in:
parent
2643c72c28
commit
be39486043
5
go.mod
5
go.mod
|
@ -2,6 +2,9 @@ module github.com/smbrave/goutil
|
||||||
|
|
||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require github.com/sirupsen/logrus v1.9.0
|
require (
|
||||||
|
github.com/sirupsen/logrus v1.9.0
|
||||||
|
gorm.io/gorm v1.25.5
|
||||||
|
)
|
||||||
|
|
||||||
require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
|
require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
package goutil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"gorm.io/gorm/logger"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type GORMLogger struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *GORMLogger) LogMode(level logger.LogLevel) logger.Interface {
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *GORMLogger) Info(context.Context, string, ...interface{}) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *GORMLogger) Warn(context.Context, string, ...interface{}) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *GORMLogger) Error(context.Context, string, ...interface{}) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *GORMLogger) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) {
|
||||||
|
sql, affects := fc()
|
||||||
|
|
||||||
|
if err != nil && err != gorm.ErrRecordNotFound {
|
||||||
|
log.Errorf("[SQL]sql=%s affect=%d cost=%dms error=%v", sql, affects, time.Since(begin).Milliseconds(), err)
|
||||||
|
} else {
|
||||||
|
log.Debugf("[SQL]sql=%s affect=%d cost=%dms", sql, affects, time.Since(begin).Milliseconds())
|
||||||
|
}
|
||||||
|
}
|
61
string.go
61
string.go
|
@ -75,3 +75,64 @@ func RandomStr(length int64) string {
|
||||||
}
|
}
|
||||||
return string(result)
|
return string(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 驼峰转蛇形 snake string
|
||||||
|
* @description XxYy to xx_yy , XxYY to xx_y_y
|
||||||
|
* @date 2020/7/30
|
||||||
|
* @param s 需要转换的字符串
|
||||||
|
* @return string
|
||||||
|
**/
|
||||||
|
|
||||||
|
func SnakeString(s string) string {
|
||||||
|
data := make([]byte, 0, len(s)*2)
|
||||||
|
j := false
|
||||||
|
num := len(s)
|
||||||
|
for i := 0; i < num; i++ {
|
||||||
|
d := s[i]
|
||||||
|
// or通过ASCII码进行大小写的转化
|
||||||
|
// 65-90(A-Z),97-122(a-z)
|
||||||
|
//判断如果字母为大写的A-Z就在前面拼接一个_
|
||||||
|
if i > 0 && d >= 'A' && d <= 'Z' && j {
|
||||||
|
data = append(data, '_')
|
||||||
|
}
|
||||||
|
if d != '_' {
|
||||||
|
j = true
|
||||||
|
}
|
||||||
|
data = append(data, d)
|
||||||
|
}
|
||||||
|
//ToLower把大写字母统一转小写
|
||||||
|
return strings.ToLower(string(data[:]))
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 蛇形转驼峰
|
||||||
|
* @description xx_yy to XxYx xx_y_y to XxYY
|
||||||
|
* @date 2020/7/30
|
||||||
|
* @param s要转换的字符串
|
||||||
|
* @return string
|
||||||
|
**/
|
||||||
|
|
||||||
|
func CamelString(s string) string {
|
||||||
|
data := make([]byte, 0, len(s))
|
||||||
|
j := false
|
||||||
|
k := false
|
||||||
|
num := len(s) - 1
|
||||||
|
for i := 0; i <= num; i++ {
|
||||||
|
d := s[i]
|
||||||
|
if k == false && d >= 'A' && d <= 'Z' {
|
||||||
|
k = true
|
||||||
|
}
|
||||||
|
if d >= 'a' && d <= 'z' && (j || k == false) {
|
||||||
|
d = d - 32
|
||||||
|
j = false
|
||||||
|
k = true
|
||||||
|
}
|
||||||
|
if k && d == '_' && num > i && s[i+1] >= 'a' && s[i+1] <= 'z' {
|
||||||
|
j = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
data = append(data, d)
|
||||||
|
}
|
||||||
|
return string(data[:])
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue