From 2f1fbe1261ce3c760747f95a345e4176db0c4ec9 Mon Sep 17 00:00:00 2001 From: SliverHorn Date: Mon, 7 Dec 2020 12:11:50 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89logger=E4=BF=9D?= =?UTF-8?q?=E7=95=99gorm=E7=9A=84=E6=A0=BC=E5=BC=8F=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E6=8E=A7=E5=88=B6=E5=8F=B0,=20=E4=BF=AE=E6=94=B9SetUserInfo?= =?UTF-8?q?=E7=9A=84=E6=A0=A1=E9=AA=8C=E8=A7=84=E5=88=99=E4=B8=BAIdVerify?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/api/v1/sys_user.go | 2 +- server/config.yaml | 2 +- server/config/gorm.go | 2 +- server/initialize/gorm.go | 58 ++++++++++++++++++++++++++++++------- server/initialize/logger.go | 33 ++++++++++++++++----- server/utils/verify.go | 2 -- 6 files changed, 75 insertions(+), 24 deletions(-) diff --git a/server/api/v1/sys_user.go b/server/api/v1/sys_user.go index c018af9e..69dc34f9 100644 --- a/server/api/v1/sys_user.go +++ b/server/api/v1/sys_user.go @@ -240,7 +240,7 @@ func DeleteUser(c *gin.Context) { func SetUserInfo(c *gin.Context) { var user model.SysUser _ = 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) return } diff --git a/server/config.yaml b/server/config.yaml index 1dbf4436..df7f9cf1 100644 --- a/server/config.yaml +++ b/server/config.yaml @@ -60,7 +60,7 @@ mysql: max-idle-conns: 10 max-open-conns: 100 log-mode: false - log-zap: false + log-zap: "" # local configuration local: diff --git a/server/config/gorm.go b/server/config/gorm.go index 1888d4c8..eb08c770 100644 --- a/server/config/gorm.go +++ b/server/config/gorm.go @@ -9,5 +9,5 @@ type Mysql struct { MaxIdleConns int `mapstructure:"max-idle-conns" json:"maxIdleConns" yaml:"max-idle-conns"` MaxOpenConns int `mapstructure:"max-open-conns" json:"maxOpenConns" yaml:"max-open-conns"` 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"` } diff --git a/server/initialize/gorm.go b/server/initialize/gorm.go index 8b87d253..07cbda00 100644 --- a/server/initialize/gorm.go +++ b/server/initialize/gorm.go @@ -10,7 +10,11 @@ import ( "os" ) -// Gorm 初始化数据库并产生数据库全局变量 +//@author: SliverHorn +//@function: Gorm +//@description: 初始化数据库并产生数据库全局变量 +//@return: *gorm.DB + func Gorm() *gorm.DB { switch global.GVA_CONFIG.System.DbType { 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) { err := db.AutoMigrate( model.SysUser{}, @@ -52,7 +61,12 @@ func MysqlTables(db *gorm.DB) { global.GVA_LOG.Info("register table success") } -// GormMysql 初始化Mysql数据库 +// +//@author: SliverHorn +//@function: GormMysql +//@description: 初始化Mysql数据库 +//@return: *gorm.DB + func GormMysql() *gorm.DB { m := global.GVA_CONFIG.Mysql dsn := m.Username + ":" + m.Password + "@tcp(" + m.Path + ")/" + m.Dbname + "?" + m.Config @@ -76,23 +90,45 @@ func GormMysql() *gorm.DB { } } -// gormConfig 根据配置决定是否开启日志 +//@author: SliverHorn +//@function: gormConfig +//@description: 根据配置决定是否开启日志 +//@param: mod bool +//@return: *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{ - Logger: Default.LogMode(logger.Info), + Logger: Default.LogMode(logger.Silent), DisableForeignKeyConstraintWhenMigrating: true, } - } - if mod { + case "Error": return &gorm.Config{ - Logger: logger.Default.LogMode(logger.Info), + Logger: Default.LogMode(logger.Error), DisableForeignKeyConstraintWhenMigrating: true, } - } else { + case "Warn": return &gorm.Config{ - Logger: logger.Default.LogMode(logger.Silent), + Logger: Default.LogMode(logger.Warn), DisableForeignKeyConstraintWhenMigrating: true, } + case "Info": + return &gorm.Config{ + Logger: Default.LogMode(logger.Info), + DisableForeignKeyConstraintWhenMigrating: true, + } + default: + if mod { + return &gorm.Config{ + Logger: logger.Default.LogMode(logger.Info), + DisableForeignKeyConstraintWhenMigrating: true, + } + } else { + return &gorm.Config{ + Logger: logger.Default.LogMode(logger.Silent), + DisableForeignKeyConstraintWhenMigrating: true, + } + } } } diff --git a/server/initialize/logger.go b/server/initialize/logger.go index a736a7d5..5da2ecf3 100644 --- a/server/initialize/logger.go +++ b/server/initialize/logger.go @@ -14,8 +14,8 @@ import ( ) 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, LogLevel: logger.Warn, Colorful: true, @@ -31,7 +31,7 @@ type traceRecorder struct { Err error } -func New(writer Writer, config GormConfig) logger.Interface { +func New(writer Writer, gormWriter logger.Writer, config GormConfig) logger.Interface { var ( infoStr = "%s\n[info] " warnStr = "%s\n[warn] " @@ -52,6 +52,7 @@ func New(writer Writer, config GormConfig) logger.Interface { return &GormLogger{ Writer: writer, + gormWriter: gormWriter, GormConfig: config, infoStr: infoStr, warnStr: warnStr, @@ -75,6 +76,7 @@ type GormConfig struct { type GormLogger struct { Writer + gormWriter logger.Writer GormConfig infoStr, warnStr, errStr string traceStr, traceErrStr, traceWarnStr string @@ -135,16 +137,31 @@ func (g *GormLogger) Trace(ctx context.Context, begin time.Time, fc func() (stri } func (g *GormLogger) Printf(message string, data ...interface{}) { + if global.GVA_CONFIG.Mysql.LogZap == "Info" && !global.GVA_CONFIG.Mysql.LogMode { + switch len(data) { + case 0: + global.GVA_LOG.Info(message) + case 1: + global.GVA_LOG.Info("gorm", zap.Any("src", data[0])) + case 2: + global.GVA_LOG.Info("gorm", zap.Any("src", data[0]), zap.Any("duration", data[1])) + case 3: + global.GVA_LOG.Info("gorm", zap.Any("src", data[0]), zap.Any("duration", data[1]), zap.Any("rows", data[2])) + 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])) + } + return + } switch len(data) { case 0: - global.GVA_LOG.Info(message) + g.gormWriter.Printf(message, "") case 1: - global.GVA_LOG.Info("gorm", zap.Any("src", data[0])) + g.gormWriter.Printf(message, data[0].(string)) case 2: - global.GVA_LOG.Info("gorm", zap.Any("src", data[0]), zap.Any("duration", data[1])) + g.gormWriter.Printf(message, data[0].(string), data[1].(float64)) case 3: - global.GVA_LOG.Info("gorm", zap.Any("src", data[0]), zap.Any("duration", data[1]), zap.Any("rows", data[2])) + g.gormWriter.Printf(message, data[0].(string), data[1].(float64), data[2].(string)) 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])) + g.gormWriter.Printf(message, data[0].(string), data[1].(float64), data[2].(string), data[3].(string)) } } diff --git a/server/utils/verify.go b/server/utils/verify.go index 8332291f..9e5796f7 100644 --- a/server/utils/verify.go +++ b/server/utils/verify.go @@ -6,12 +6,10 @@ var ( MenuVerify = Rules{"Path": {NotEmpty()}, "ParentId": {NotEmpty()}, "Name": {NotEmpty()}, "Component": {NotEmpty()}, "Sort": {Ge("0")}} MenuMetaVerify = Rules{"Title": {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()}} PageInfoVerify = Rules{"Page": {NotEmpty()}, "PageSize": {NotEmpty()}} CustomerVerify = Rules{"CustomerName": {NotEmpty()}, "CustomerPhoneData": {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()}} AuthorityIdVerify = Rules{"AuthorityId": {NotEmpty()}} OldAuthorityVerify = Rules{"OldAuthorityId": {NotEmpty()}}