Browse Source

Merge pull request #306 from SliverHorn/sliverhorn_dev

自定义logger保留gorm的格式输出控制台, 修改SetUserInfo的校验规则为IdVerify
main
奇淼(piexlmax 4 years ago
committed by GitHub
parent
commit
71073ae8d8
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      server/api/v1/sys_user.go
  2. 2
      server/config.yaml
  3. 2
      server/config/gorm.go
  4. 48
      server/initialize/gorm.go
  5. 23
      server/initialize/logger.go
  6. 2
      server/utils/verify.go

2
server/api/v1/sys_user.go

@ -240,7 +240,7 @@ func DeleteUser(c *gin.Context) {
func SetUserInfo(c *gin.Context) { func SetUserInfo(c *gin.Context) {
var user model.SysUser var user model.SysUser
_ = c.ShouldBindJSON(&user) _ = c.ShouldBindJSON(&user)
if err := utils.Verify(user, utils.SetUserVerify); err != nil {
if err := utils.Verify(user, utils.IdVerify); err != nil {
response.FailWithMessage(err.Error(), c) response.FailWithMessage(err.Error(), c)
return return
} }

2
server/config.yaml

@ -60,7 +60,7 @@ mysql:
max-idle-conns: 10 max-idle-conns: 10
max-open-conns: 100 max-open-conns: 100
log-mode: false log-mode: false
log-zap: false
log-zap: ""
# local configuration # local configuration
local: local:

2
server/config/gorm.go

@ -9,5 +9,5 @@ type Mysql struct {
MaxIdleConns int `mapstructure:"max-idle-conns" json:"maxIdleConns" yaml:"max-idle-conns"` MaxIdleConns int `mapstructure:"max-idle-conns" json:"maxIdleConns" yaml:"max-idle-conns"`
MaxOpenConns int `mapstructure:"max-open-conns" json:"maxOpenConns" yaml:"max-open-conns"` MaxOpenConns int `mapstructure:"max-open-conns" json:"maxOpenConns" yaml:"max-open-conns"`
LogMode bool `mapstructure:"log-mode" json:"logMode" yaml:"log-mode"` LogMode bool `mapstructure:"log-mode" json:"logMode" yaml:"log-mode"`
LogZap bool `mapstructure:"log-zap" json:"logZap" yaml:"log-zap"`
LogZap string `mapstructure:"log-zap" json:"logZap" yaml:"log-zap"`
} }

48
server/initialize/gorm.go

@ -10,7 +10,11 @@ import (
"os" "os"
) )
// Gorm 初始化数据库并产生数据库全局变量
//@author: SliverHorn
//@function: Gorm
//@description: 初始化数据库并产生数据库全局变量
//@return: *gorm.DB
func Gorm() *gorm.DB { func Gorm() *gorm.DB {
switch global.GVA_CONFIG.System.DbType { switch global.GVA_CONFIG.System.DbType {
case "mysql": case "mysql":
@ -20,7 +24,12 @@ func Gorm() *gorm.DB {
} }
} }
// MysqlTables 注册数据库表专用
// MysqlTables
//@author: SliverHorn
//@function: MysqlTables
//@description: 注册数据库表专用
//@param: db *gorm.DB
func MysqlTables(db *gorm.DB) { func MysqlTables(db *gorm.DB) {
err := db.AutoMigrate( err := db.AutoMigrate(
model.SysUser{}, model.SysUser{},
@ -52,7 +61,12 @@ func MysqlTables(db *gorm.DB) {
global.GVA_LOG.Info("register table success") global.GVA_LOG.Info("register table success")
} }
// GormMysql 初始化Mysql数据库
//
//@author: SliverHorn
//@function: GormMysql
//@description: 初始化Mysql数据库
//@return: *gorm.DB
func GormMysql() *gorm.DB { func GormMysql() *gorm.DB {
m := global.GVA_CONFIG.Mysql m := global.GVA_CONFIG.Mysql
dsn := m.Username + ":" + m.Password + "@tcp(" + m.Path + ")/" + m.Dbname + "?" + m.Config dsn := m.Username + ":" + m.Password + "@tcp(" + m.Path + ")/" + m.Dbname + "?" + m.Config
@ -76,14 +90,35 @@ func GormMysql() *gorm.DB {
} }
} }
// gormConfig 根据配置决定是否开启日志
//@author: SliverHorn
//@function: gormConfig
//@description: 根据配置决定是否开启日志
//@param: mod bool
//@return: *gorm.Config
func gormConfig(mod bool) *gorm.Config { func gormConfig(mod bool) *gorm.Config {
if global.GVA_CONFIG.Mysql.LogZap {
switch global.GVA_CONFIG.Mysql.LogZap {
case "Silent":
return &gorm.Config{ return &gorm.Config{
Logger: Default.LogMode(logger.Info),
Logger: Default.LogMode(logger.Silent),
DisableForeignKeyConstraintWhenMigrating: true,
}
case "Error":
return &gorm.Config{
Logger: Default.LogMode(logger.Error),
DisableForeignKeyConstraintWhenMigrating: true, DisableForeignKeyConstraintWhenMigrating: true,
} }
case "Warn":
return &gorm.Config{
Logger: Default.LogMode(logger.Warn),
DisableForeignKeyConstraintWhenMigrating: true,
}
case "Info":
return &gorm.Config{
Logger: Default.LogMode(logger.Info),
DisableForeignKeyConstraintWhenMigrating: true,
} }
default:
if mod { if mod {
return &gorm.Config{ return &gorm.Config{
Logger: logger.Default.LogMode(logger.Info), Logger: logger.Default.LogMode(logger.Info),
@ -95,4 +130,5 @@ func gormConfig(mod bool) *gorm.Config {
DisableForeignKeyConstraintWhenMigrating: true, DisableForeignKeyConstraintWhenMigrating: true,
} }
} }
}
} }

23
server/initialize/logger.go

@ -14,8 +14,8 @@ import (
) )
var ( var (
Discard = New(log.New(ioutil.Discard, "", log.LstdFlags), GormConfig{})
Default = New(log.New(os.Stdout, "\r\n", log.LstdFlags), GormConfig{
Discard = New(log.New(ioutil.Discard, "", log.LstdFlags), log.New(ioutil.Discard, "", log.LstdFlags), GormConfig{})
Default = New(log.New(os.Stdout, "\r\n", log.LstdFlags), log.New(os.Stdout, "\r\n", log.LstdFlags), GormConfig{
SlowThreshold: 200 * time.Millisecond, SlowThreshold: 200 * time.Millisecond,
LogLevel: logger.Warn, LogLevel: logger.Warn,
Colorful: true, Colorful: true,
@ -31,7 +31,7 @@ type traceRecorder struct {
Err error Err error
} }
func New(writer Writer, config GormConfig) logger.Interface {
func New(writer Writer, gormWriter logger.Writer, config GormConfig) logger.Interface {
var ( var (
infoStr = "%s\n[info] " infoStr = "%s\n[info] "
warnStr = "%s\n[warn] " warnStr = "%s\n[warn] "
@ -52,6 +52,7 @@ func New(writer Writer, config GormConfig) logger.Interface {
return &GormLogger{ return &GormLogger{
Writer: writer, Writer: writer,
gormWriter: gormWriter,
GormConfig: config, GormConfig: config,
infoStr: infoStr, infoStr: infoStr,
warnStr: warnStr, warnStr: warnStr,
@ -75,6 +76,7 @@ type GormConfig struct {
type GormLogger struct { type GormLogger struct {
Writer Writer
gormWriter logger.Writer
GormConfig GormConfig
infoStr, warnStr, errStr string infoStr, warnStr, errStr string
traceStr, traceErrStr, traceWarnStr string traceStr, traceErrStr, traceWarnStr string
@ -135,6 +137,7 @@ func (g *GormLogger) Trace(ctx context.Context, begin time.Time, fc func() (stri
} }
func (g *GormLogger) Printf(message string, data ...interface{}) { func (g *GormLogger) Printf(message string, data ...interface{}) {
if global.GVA_CONFIG.Mysql.LogZap == "Info" && !global.GVA_CONFIG.Mysql.LogMode {
switch len(data) { switch len(data) {
case 0: case 0:
global.GVA_LOG.Info(message) global.GVA_LOG.Info(message)
@ -147,4 +150,18 @@ func (g *GormLogger) Printf(message string, data ...interface{}) {
case 4: case 4:
global.GVA_LOG.Info("gorm", zap.Any("src", data[0]), zap.Any("duration", data[1]), zap.Any("rows", data[2]), zap.Any("sql", data[3])) global.GVA_LOG.Info("gorm", zap.Any("src", data[0]), zap.Any("duration", data[1]), zap.Any("rows", data[2]), zap.Any("sql", data[3]))
} }
return
}
switch len(data) {
case 0:
g.gormWriter.Printf(message, "")
case 1:
g.gormWriter.Printf(message, data[0].(string))
case 2:
g.gormWriter.Printf(message, data[0].(string), data[1].(float64))
case 3:
g.gormWriter.Printf(message, data[0].(string), data[1].(float64), data[2].(string))
case 4:
g.gormWriter.Printf(message, data[0].(string), data[1].(float64), data[2].(string), data[3].(string))
}
} }

2
server/utils/verify.go

@ -6,12 +6,10 @@ var (
MenuVerify = Rules{"Path": {NotEmpty()}, "ParentId": {NotEmpty()}, "Name": {NotEmpty()}, "Component": {NotEmpty()}, "Sort": {Ge("0")}} MenuVerify = Rules{"Path": {NotEmpty()}, "ParentId": {NotEmpty()}, "Name": {NotEmpty()}, "Component": {NotEmpty()}, "Sort": {Ge("0")}}
MenuMetaVerify = Rules{"Title": {NotEmpty()}} MenuMetaVerify = Rules{"Title": {NotEmpty()}}
LoginVerify = Rules{"CaptchaId": {NotEmpty()}, "Captcha": {NotEmpty()}, "Username": {NotEmpty()}, "Password": {NotEmpty()}} LoginVerify = Rules{"CaptchaId": {NotEmpty()}, "Captcha": {NotEmpty()}, "Username": {NotEmpty()}, "Password": {NotEmpty()}}
SetUserVerify = Rules{"ID": {NotEmpty()}, "Username": {NotEmpty()}, "NickName": {NotEmpty()}, "HeaderImg": {NotEmpty()}}
RegisterVerify = Rules{"Username": {NotEmpty()}, "NickName": {NotEmpty()}, "Password": {NotEmpty()}, "AuthorityId": {NotEmpty()}} RegisterVerify = Rules{"Username": {NotEmpty()}, "NickName": {NotEmpty()}, "Password": {NotEmpty()}, "AuthorityId": {NotEmpty()}}
PageInfoVerify = Rules{"Page": {NotEmpty()}, "PageSize": {NotEmpty()}} PageInfoVerify = Rules{"Page": {NotEmpty()}, "PageSize": {NotEmpty()}}
CustomerVerify = Rules{"CustomerName": {NotEmpty()}, "CustomerPhoneData": {NotEmpty()}} CustomerVerify = Rules{"CustomerName": {NotEmpty()}, "CustomerPhoneData": {NotEmpty()}}
AutoCodeVerify = Rules{"Abbreviation": {NotEmpty()}, "StructName": {NotEmpty()}, "PackageName": {NotEmpty()}, "Fields": {NotEmpty()}} AutoCodeVerify = Rules{"Abbreviation": {NotEmpty()}, "StructName": {NotEmpty()}, "PackageName": {NotEmpty()}, "Fields": {NotEmpty()}}
WorkFlowVerify = Rules{"WorkflowNickName": {NotEmpty()}, "WorkflowName": {NotEmpty()}, "WorkflowDescription": {NotEmpty()}, "WorkflowStepInfo": {NotEmpty()}}
AuthorityVerify = Rules{"AuthorityId": {NotEmpty()}, "AuthorityName": {NotEmpty()}, "ParentId": {NotEmpty()}} AuthorityVerify = Rules{"AuthorityId": {NotEmpty()}, "AuthorityName": {NotEmpty()}, "ParentId": {NotEmpty()}}
AuthorityIdVerify = Rules{"AuthorityId": {NotEmpty()}} AuthorityIdVerify = Rules{"AuthorityId": {NotEmpty()}}
OldAuthorityVerify = Rules{"OldAuthorityId": {NotEmpty()}} OldAuthorityVerify = Rules{"OldAuthorityId": {NotEmpty()}}

Loading…
Cancel
Save