🎨 优化gorm日志
This commit is contained in:
parent
ebae5c29cd
commit
40a3dc9339
7
gorm.go
7
gorm.go
@ -79,10 +79,15 @@ func NewGormLoggerWithConfig(config gl.Config) gl.Interface {
|
|||||||
|
|
||||||
// DefaultGormLogger 默认的日志实现
|
// DefaultGormLogger 默认的日志实现
|
||||||
func DefaultGormLogger() gl.Interface {
|
func DefaultGormLogger() gl.Interface {
|
||||||
|
// 默认日志级别为Info,如果是生产环境,就是Error
|
||||||
|
logLevel := gl.Info
|
||||||
|
if config.Mode == Prod {
|
||||||
|
logLevel = gl.Error
|
||||||
|
}
|
||||||
return &gormLogger{gl.Config{
|
return &gormLogger{gl.Config{
|
||||||
SlowThreshold: time.Second, // Slow SQL threshold
|
SlowThreshold: time.Second, // Slow SQL threshold
|
||||||
IgnoreRecordNotFoundError: false, // 忽略没找到结果的错误
|
IgnoreRecordNotFoundError: false, // 忽略没找到结果的错误
|
||||||
LogLevel: gl.Info, // Log level
|
LogLevel: logLevel, // Log level
|
||||||
Colorful: false, // Disable color
|
Colorful: false, // Disable color
|
||||||
}}
|
}}
|
||||||
}
|
}
|
||||||
|
22
gorm_test.go
22
gorm_test.go
@ -4,7 +4,9 @@ import (
|
|||||||
"gitee.ltd/lxh/logger/log"
|
"gitee.ltd/lxh/logger/log"
|
||||||
"gorm.io/driver/mysql"
|
"gorm.io/driver/mysql"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
gl "gorm.io/gorm/logger"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGormLogger(t *testing.T) {
|
func TestGormLogger(t *testing.T) {
|
||||||
@ -15,6 +17,26 @@ func TestGormLogger(t *testing.T) {
|
|||||||
log.Panicf("mysql connect error: %s", err.Error())
|
log.Panicf("mysql connect error: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var count int64
|
||||||
|
if err := engine.Table("t_tenant").Count(&count).Error; err != nil {
|
||||||
|
t.Log(err)
|
||||||
|
}
|
||||||
|
t.Logf("count: %d", count)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGormLoggerWithConfig(t *testing.T) {
|
||||||
|
dsn := "saas:saas123@tcp(10.11.0.10:3307)/saas_tenant?charset=utf8mb4&parseTime=True&loc=Local"
|
||||||
|
|
||||||
|
engine, err := gorm.Open(mysql.Open(dsn), &gorm.Config{Logger: NewGormLoggerWithConfig(gl.Config{
|
||||||
|
SlowThreshold: time.Second, // Slow SQL threshold
|
||||||
|
IgnoreRecordNotFoundError: false, // 忽略没找到结果的错误
|
||||||
|
LogLevel: gl.Warn, // Log level
|
||||||
|
Colorful: false, // Disable color
|
||||||
|
})})
|
||||||
|
if err != nil {
|
||||||
|
log.Panicf("mysql connect error: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
var count int64
|
var count int64
|
||||||
if err := engine.Table("t_tenant1").Count(&count).Error; err != nil {
|
if err := engine.Table("t_tenant1").Count(&count).Error; err != nil {
|
||||||
t.Log(err)
|
t.Log(err)
|
||||||
|
Loading…
Reference in New Issue
Block a user