From 1000c75a55dd05bffb7279d6563341fd7c6a17c8 Mon Sep 17 00:00:00 2001 From: qloog Date: Fri, 19 Jul 2024 22:39:36 +0800 Subject: [PATCH] chore: optimize log and orm --- examples/log/logger.yaml | 9 +++++---- examples/log/main.go | 2 +- pkg/log/config.go | 3 ++- pkg/log/logger.go | 11 +++++++---- pkg/log/options.go | 2 +- pkg/log/zap.go | 10 +++++----- pkg/middleware/cors.go | 5 +---- pkg/storage/orm/orm.go | 7 ++----- 8 files changed, 24 insertions(+), 25 deletions(-) diff --git a/examples/log/logger.yaml b/examples/log/logger.yaml index dc5d53d089..47a7b6359e 100644 --- a/examples/log/logger.yaml +++ b/examples/log/logger.yaml @@ -1,10 +1,11 @@ Development: false DisableCaller: false DisableStacktrace: false -Encoding: json # json or console -Level: info # 日志级别,INFO, WARN, ERROR -Name: eagle -Writers: file # 有2个可选项:file,console 选择file会将日志记录到logger_file指定的日志文件中,选择console会将日志输出到标准输出,当然也可以两者同时选择 +Encoding: json # json or console +Level: info # 日志级别,INFO, WARN, ERROR +ServiceName: demo-service +Filename: demo-file +Writers: file # 有2个可选项:file,console 选择file会将日志记录到logger_file指定的日志文件中,选择console会将日志输出到标准输出,当然也可以两者同时选择 LoggerDir: /tmp/logs LogRollingPolicy: daily LogRotateDate: 1 diff --git a/examples/log/main.go b/examples/log/main.go index 6d6b7d90a6..45ca1ffbf1 100644 --- a/examples/log/main.go +++ b/examples/log/main.go @@ -14,7 +14,7 @@ func main() { log.Info("test log") // print log using custom filename - log.Init(log.WithFilename("custom")) + log.Init(log.WithFilename("custom-filename")) log.Info("test log with filename") // print log using custom dir and filename diff --git a/pkg/log/config.go b/pkg/log/config.go index d900c98c57..aa31bce2ef 100644 --- a/pkg/log/config.go +++ b/pkg/log/config.go @@ -7,7 +7,8 @@ type Config struct { DisableStacktrace bool Encoding string Level string - Name string // service name + ServiceName string // service name + Fileanme string Writers string LoggerDir string LogFormatText bool diff --git a/pkg/log/logger.go b/pkg/log/logger.go index c7638a69a8..e58d90f103 100644 --- a/pkg/log/logger.go +++ b/pkg/log/logger.go @@ -144,11 +144,14 @@ func Errorf(format string, args ...interface{}) { // WithFields logger // output more field, eg: -// contextLogger := log.WithFields(log.Fields{"key1": "value1"}) -// contextLogger.Info("print multi field") +// +// contextLogger := log.WithFields(log.Fields{"key1": "value1"}) +// contextLogger.Info("print multi field") +// // or more sample to use: -// log.WithFields(log.Fields{"key1": "value1"}).Info("this is a test log") -// log.WithFields(log.Fields{"key1": "value1"}).Infof("this is a test log, user_id: %d", userID) +// +// log.WithFields(log.Fields{"key1": "value1"}).Info("this is a test log") +// log.WithFields(log.Fields{"key1": "value1"}).Infof("this is a test log, user_id: %d", userID) func WithFields(keyValues Fields) Logger { return GetLogger().WithFields(keyValues) } diff --git a/pkg/log/options.go b/pkg/log/options.go index 85d0f1a4ef..2bdec5f451 100644 --- a/pkg/log/options.go +++ b/pkg/log/options.go @@ -5,7 +5,7 @@ type Option func(*Config) // WithFilename set log filename func WithFilename(filename string) Option { return func(cfg *Config) { - cfg.Name = filename + cfg.Fileanme = filename } } diff --git a/pkg/log/zap.go b/pkg/log/zap.go index 5f9a6a557e..90d65ddd7c 100644 --- a/pkg/log/zap.go +++ b/pkg/log/zap.go @@ -118,7 +118,7 @@ func buildLogger(cfg *Config, skip int) *zap.Logger { hostname, _ = os.Hostname() option := zap.Fields( zap.String("ip", utils.GetLocalIP()), - zap.String("app_id", cfg.Name), + zap.String("app_id", cfg.ServiceName), zap.String("instance_id", hostname), ) options = append(options, option) @@ -170,7 +170,7 @@ func buildLogger(cfg *Config, skip int) *zap.Logger { } func getAllCore(encoder zapcore.Encoder, cfg *Config) zapcore.Core { - allWriter := getLogWriterWithTime(cfg, GetLogFile(cfg.Name, logSuffix)) + allWriter := getLogWriterWithTime(cfg, GetLogFile(cfg.Fileanme, logSuffix)) allLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool { return lvl <= zapcore.FatalLevel }) @@ -178,7 +178,7 @@ func getAllCore(encoder zapcore.Encoder, cfg *Config) zapcore.Core { } func getInfoCore(encoder zapcore.Encoder, cfg *Config) zapcore.Core { - infoWrite := getLogWriterWithTime(cfg, GetLogFile(cfg.Name, logSuffix)) + infoWrite := getLogWriterWithTime(cfg, GetLogFile(cfg.Fileanme, logSuffix)) infoLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool { return lvl <= zapcore.InfoLevel }) @@ -186,7 +186,7 @@ func getInfoCore(encoder zapcore.Encoder, cfg *Config) zapcore.Core { } func getWarnCore(encoder zapcore.Encoder, cfg *Config) (zapcore.Core, zap.Option) { - warnWrite := getLogWriterWithTime(cfg, GetLogFile(cfg.Name, warnLogSuffix)) + warnWrite := getLogWriterWithTime(cfg, GetLogFile(cfg.Fileanme, warnLogSuffix)) var stacktrace zap.Option warnLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool { if !cfg.DisableCaller { @@ -200,7 +200,7 @@ func getWarnCore(encoder zapcore.Encoder, cfg *Config) (zapcore.Core, zap.Option } func getErrorCore(encoder zapcore.Encoder, cfg *Config) (zapcore.Core, zap.Option) { - errorFilename := GetLogFile(cfg.Name, errorLogSuffix) + errorFilename := GetLogFile(cfg.Fileanme, errorLogSuffix) errorWrite := getLogWriterWithTime(cfg, errorFilename) var stacktrace zap.Option errorLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool { diff --git a/pkg/middleware/cors.go b/pkg/middleware/cors.go index 6d0c607cd5..66f4310fee 100644 --- a/pkg/middleware/cors.go +++ b/pkg/middleware/cors.go @@ -19,9 +19,6 @@ func Cors() gin.HandlerFunc { AllowHeaders: []string{"Origin", "Authorization", "Content-Type", "Accept"}, ExposeHeaders: []string{"Content-Length"}, AllowCredentials: true, - AllowOriginFunc: func(origin string) bool { - return origin == "https://github.com" - }, - MaxAge: maxAge * time.Hour, + MaxAge: maxAge * time.Hour, }) } diff --git a/pkg/storage/orm/orm.go b/pkg/storage/orm/orm.go index 1622f0a363..5b2b8fa7d3 100644 --- a/pkg/storage/orm/orm.go +++ b/pkg/storage/orm/orm.go @@ -197,21 +197,18 @@ func LoadConf(name string) (ret *Config, err error) { // getDSN return dsn string func getDSN(c *Config) string { // default mysql - dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=%t&loc=%s&timeout=%s%readTimeout=%s%writeTimeout=%s", + dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true&loc=Local&timeout=%s&readTimeout=%s&writeTimeout=%s", c.UserName, c.Password, c.Addr, c.Name, - true, - //"Asia/Shanghai"), - "Local", c.Timeout, c.ReadTimeout, c.WriteTimeout, ) if c.Driver == DriverPostgres { - dsn = fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable&connect_timeout=%s%statement_timeout=%s", + dsn = fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable&connect_timeout=%s&statement_timeout=%s", c.UserName, c.Password, c.Addr,