From 357591404849cb06afea9a99a651e449aceba8a5 Mon Sep 17 00:00:00 2001 From: Richard Date: Fri, 19 Jul 2024 22:59:28 +0800 Subject: [PATCH] chore: optimize log and orm (#144) * chore: optimize log and orm --- CHANGELOG.md | 1 + config/docker/logger.yaml | 9 +++++---- config/local/logger.yaml | 9 +++++---- pkg/log/config.go | 2 +- pkg/log/options.go | 2 +- pkg/log/zap.go | 8 ++++---- 6 files changed, 17 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 072748c85..ecd415a65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## v1.9.0 - feat: support clickhouse - chore: upgrade go to v1.21 +- chore: optimize log and orm ## v1.8.2 - feat: support PostgreSQL diff --git a/config/docker/logger.yaml b/config/docker/logger.yaml index 9463e2fce..38698a350 100644 --- a/config/docker/logger.yaml +++ b/config/docker/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: console # 有2个可选项:file,console 选择file会将日志记录到logger_file指定的日志文件中,选择console会将日志输出到标准输出,当然也可以两者同时选择 +Encoding: json # json or console +Level: info # 日志级别,INFO, WARN, ERROR +ServiceName: user-service +Filename: eagle +Writers: console # 有2个可选项:file,console 选择file会将日志记录到logger_file指定的日志文件中,选择console会将日志输出到标准输出,当然也可以两者同时选择 LoggerDir: /data/logs LogRollingPolicy: daily LogRotateDate: 1 diff --git a/config/local/logger.yaml b/config/local/logger.yaml index 9463e2fce..38698a350 100644 --- a/config/local/logger.yaml +++ b/config/local/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: console # 有2个可选项:file,console 选择file会将日志记录到logger_file指定的日志文件中,选择console会将日志输出到标准输出,当然也可以两者同时选择 +Encoding: json # json or console +Level: info # 日志级别,INFO, WARN, ERROR +ServiceName: user-service +Filename: eagle +Writers: console # 有2个可选项:file,console 选择file会将日志记录到logger_file指定的日志文件中,选择console会将日志输出到标准输出,当然也可以两者同时选择 LoggerDir: /data/logs LogRollingPolicy: daily LogRotateDate: 1 diff --git a/pkg/log/config.go b/pkg/log/config.go index aa31bce2e..32c88fbf9 100644 --- a/pkg/log/config.go +++ b/pkg/log/config.go @@ -8,7 +8,7 @@ type Config struct { Encoding string Level string ServiceName string // service name - Fileanme string + Filename string Writers string LoggerDir string LogFormatText bool diff --git a/pkg/log/options.go b/pkg/log/options.go index 2bdec5f45..68cf10eb6 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.Fileanme = filename + cfg.Filename = filename } } diff --git a/pkg/log/zap.go b/pkg/log/zap.go index 90d65ddd7..6aab4de7f 100644 --- a/pkg/log/zap.go +++ b/pkg/log/zap.go @@ -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.Fileanme, logSuffix)) + allWriter := getLogWriterWithTime(cfg, GetLogFile(cfg.Filename, 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.Fileanme, logSuffix)) + infoWrite := getLogWriterWithTime(cfg, GetLogFile(cfg.Filename, 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.Fileanme, warnLogSuffix)) + warnWrite := getLogWriterWithTime(cfg, GetLogFile(cfg.Filename, 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.Fileanme, errorLogSuffix) + errorFilename := GetLogFile(cfg.Filename, errorLogSuffix) errorWrite := getLogWriterWithTime(cfg, errorFilename) var stacktrace zap.Option errorLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {