-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1.config gorm相关移除DBBASE实现改为GeneralDB实现LogLevel函数 2.移除initializel/internal/logger实现,改为gorm_logger_writer实现gorm的logger通过zap输出到文件或者控制台 3.优化initializel/internal/gorm代码 Co-authored-by: SliverHorn <caiwei.lai@jutze.com.cn>
- Loading branch information
1 parent
02fc503
commit 23952c5
Showing
9 changed files
with
85 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,48 @@ | ||
package internal | ||
|
||
import ( | ||
"github.com/flipped-aurora/gin-vue-admin/server/config" | ||
"github.com/flipped-aurora/gin-vue-admin/server/global" | ||
"gorm.io/gorm" | ||
"gorm.io/gorm/logger" | ||
"gorm.io/gorm/schema" | ||
"log" | ||
"os" | ||
"time" | ||
) | ||
|
||
type DBBASE interface { | ||
GetLogMode() string | ||
} | ||
|
||
var Gorm = new(_gorm) | ||
|
||
type _gorm struct{} | ||
|
||
// Config gorm 自定义配置 | ||
// Author [SliverHorn](https://github.com/SliverHorn) | ||
func (g *_gorm) Config(prefix string, singular bool) *gorm.Config { | ||
config := &gorm.Config{ | ||
NamingStrategy: schema.NamingStrategy{ | ||
TablePrefix: prefix, | ||
SingularTable: singular, | ||
}, | ||
DisableForeignKeyConstraintWhenMigrating: true, | ||
} | ||
_default := NewZapLogger() | ||
var logMode DBBASE | ||
var general config.GeneralDB | ||
switch global.GVA_CONFIG.System.DbType { | ||
case "mysql": | ||
logMode = &global.GVA_CONFIG.Mysql | ||
general = global.GVA_CONFIG.Mysql.GeneralDB | ||
case "pgsql": | ||
logMode = &global.GVA_CONFIG.Pgsql | ||
general = global.GVA_CONFIG.Pgsql.GeneralDB | ||
case "oracle": | ||
logMode = &global.GVA_CONFIG.Oracle | ||
general = global.GVA_CONFIG.Oracle.GeneralDB | ||
case "sqlite": | ||
general = global.GVA_CONFIG.Sqlite.GeneralDB | ||
case "mssql": | ||
general = global.GVA_CONFIG.Mssql.GeneralDB | ||
default: | ||
logMode = &global.GVA_CONFIG.Mysql | ||
general = global.GVA_CONFIG.Mysql.GeneralDB | ||
} | ||
|
||
switch logMode.GetLogMode() { | ||
case "silent", "Silent": | ||
config.Logger = _default.LogMode(logger.Silent) | ||
case "error", "Error": | ||
config.Logger = _default.LogMode(logger.Error) | ||
case "warn", "Warn": | ||
config.Logger = _default.LogMode(logger.Warn) | ||
case "info", "Info": | ||
config.Logger = _default.LogMode(logger.Info) | ||
default: | ||
config.Logger = _default.LogMode(logger.Info) | ||
return &gorm.Config{ | ||
Logger: logger.New(NewWriter(general, log.New(os.Stdout, "\r\n", log.LstdFlags)), logger.Config{ | ||
SlowThreshold: 200 * time.Millisecond, | ||
LogLevel: general.LogLevel(), | ||
Colorful: true, | ||
}), | ||
NamingStrategy: schema.NamingStrategy{ | ||
TablePrefix: prefix, | ||
SingularTable: singular, | ||
}, | ||
DisableForeignKeyConstraintWhenMigrating: true, | ||
} | ||
return config | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package internal | ||
|
||
import ( | ||
"fmt" | ||
"github.com/flipped-aurora/gin-vue-admin/server/config" | ||
"go.uber.org/zap" | ||
"gorm.io/gorm/logger" | ||
) | ||
|
||
type Writer struct { | ||
config config.GeneralDB | ||
writer logger.Writer | ||
} | ||
|
||
func NewWriter(config config.GeneralDB, writer logger.Writer) *Writer { | ||
return &Writer{config: config, writer: writer} | ||
} | ||
|
||
// Printf 格式化打印日志 | ||
func (c *Writer) Printf(message string, data ...any) { | ||
if c.config.LogZap { | ||
switch c.config.LogLevel() { | ||
case logger.Silent: | ||
zap.L().Debug(fmt.Sprintf(message, data...)) | ||
case logger.Error: | ||
zap.L().Error(fmt.Sprintf(message, data...)) | ||
case logger.Warn: | ||
zap.L().Warn(fmt.Sprintf(message, data...)) | ||
case logger.Info: | ||
zap.L().Info(fmt.Sprintf(message, data...)) | ||
default: | ||
zap.L().Info(fmt.Sprintf(message, data...)) | ||
} | ||
return | ||
} | ||
c.writer.Printf(message, data...) | ||
} |
This file was deleted.
Oops, something went wrong.