Skip to content

Commit

Permalink
chore: optimize log and orm (#144)
Browse files Browse the repository at this point in the history
* chore: optimize log and orm
  • Loading branch information
qloog committed Jul 19, 2024
1 parent c776690 commit 3575914
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions config/docker/logger.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 5 additions & 4 deletions config/local/logger.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/log/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/log/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/log/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,23 +170,23 @@ 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
})
return zapcore.NewCore(encoder, zapcore.AddSync(allWriter), allLevel)
}

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
})
return zapcore.NewCore(encoder, zapcore.AddSync(infoWrite), infoLevel)
}

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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 3575914

Please sign in to comment.