Skip to content

Commit

Permalink
Update: (#1758)
Browse files Browse the repository at this point in the history
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
SliverHorn and SliverHorn authored May 27, 2024
1 parent 02fc503 commit 23952c5
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 119 deletions.
24 changes: 22 additions & 2 deletions server/config/db_list.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package config

import (
"gorm.io/gorm/logger"
"strings"
)

type DsnProvider interface {
Dsn() string
}
Expand All @@ -16,14 +21,29 @@ type GeneralDB struct {
Username string `mapstructure:"username" json:"username" yaml:"username"` // 数据库密码
Password string `mapstructure:"password" json:"password" yaml:"password"` // 数据库密码
Path string `mapstructure:"path" json:"path" yaml:"path"`
Engine string `mapstructure:"engine" json:"engine" yaml:"engine" default:"InnoDB"` //数据库引擎,默认InnoDB
Engine string `mapstructure:"engine" json:"engine" yaml:"engine" default:"InnoDB"` // 数据库引擎,默认InnoDB
LogMode string `mapstructure:"log-mode" json:"log-mode" yaml:"log-mode"` // 是否开启Gorm全局日志
MaxIdleConns int `mapstructure:"max-idle-conns" json:"max-idle-conns" yaml:"max-idle-conns"` // 空闲中的最大连接数
MaxOpenConns int `mapstructure:"max-open-conns" json:"max-open-conns" yaml:"max-open-conns"` // 打开到数据库的最大连接数
Singular bool `mapstructure:"singular" json:"singular" yaml:"singular"` //是否开启全局禁用复数,true表示开启
Singular bool `mapstructure:"singular" json:"singular" yaml:"singular"` // 是否开启全局禁用复数,true表示开启
LogZap bool `mapstructure:"log-zap" json:"log-zap" yaml:"log-zap"` // 是否通过zap写入日志文件
}

func (c GeneralDB) LogLevel() logger.LogLevel {
switch strings.ToLower(c.LogMode) {
case "silent", "Silent":
return logger.Silent
case "error", "Error":
return logger.Error
case "warn", "Warn":
return logger.Warn
case "info", "Info":
return logger.Info
default:
return logger.Info
}
}

type SpecializedDB struct {
Type string `mapstructure:"type" json:"type" yaml:"type"`
AliasName string `mapstructure:"alias-name" json:"alias-name" yaml:"alias-name"`
Expand Down
7 changes: 2 additions & 5 deletions server/config/gorm_mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ package config
type Mssql struct {
GeneralDB `yaml:",inline" mapstructure:",squash"`
}
//dsn := "sqlserver://gorm:LoremIpsum86@localhost:9930?database=gorm"

// Dsn "sqlserver://gorm:LoremIpsum86@localhost:9930?database=gorm"
func (m *Mssql) Dsn() string {
return "sqlserver://" + m.Username + ":" + m.Password + "@" + m.Path + ":" + m.Port + "?database=" + m.Dbname + "&encrypt=disable"
}

func (m *Mssql) GetLogMode() string {
return m.LogMode
}
4 changes: 0 additions & 4 deletions server/config/gorm_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,3 @@ type Mysql struct {
func (m *Mysql) Dsn() string {
return m.Username + ":" + m.Password + "@tcp(" + m.Path + ":" + m.Port + ")/" + m.Dbname + "?" + m.Config
}

func (m *Mysql) GetLogMode() string {
return m.LogMode
}
4 changes: 0 additions & 4 deletions server/config/gorm_oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,3 @@ func (m *Oracle) Dsn() string {
return "oracle://" + m.Username + ":" + m.Password + "@" + m.Path + ":" + m.Port + "/" + m.Dbname + "?" + m.Config

}

func (m *Oracle) GetLogMode() string {
return m.LogMode
}
4 changes: 0 additions & 4 deletions server/config/gorm_pgsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,3 @@ func (p *Pgsql) Dsn() string {
func (p *Pgsql) LinkDsn(dbname string) string {
return "host=" + p.Path + " user=" + p.Username + " password=" + p.Password + " dbname=" + dbname + " port=" + p.Port + " " + p.Config
}

func (m *Pgsql) GetLogMode() string {
return m.LogMode
}
4 changes: 0 additions & 4 deletions server/config/gorm_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,3 @@ type Sqlite struct {
func (s *Sqlite) Dsn() string {
return filepath.Join(s.Path, s.Dbname+".db")
}

func (s *Sqlite) GetLogMode() string {
return s.LogMode
}
54 changes: 24 additions & 30 deletions server/initialize/internal/gorm.go
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
}
37 changes: 37 additions & 0 deletions server/initialize/internal/gorm_logger_writer.go
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...)
}
66 changes: 0 additions & 66 deletions server/initialize/internal/logger.go

This file was deleted.

0 comments on commit 23952c5

Please sign in to comment.