From eda944cb253763622ecc63045daeba414c2bcee3 Mon Sep 17 00:00:00 2001 From: qloog Date: Sun, 20 Oct 2024 17:10:49 +0800 Subject: [PATCH 1/4] feat: add logger writer for gorm --- pkg/storage/orm/log_writer.go | 22 ++++++++++++++++++++++ pkg/storage/orm/orm.go | 14 ++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 pkg/storage/orm/log_writer.go diff --git a/pkg/storage/orm/log_writer.go b/pkg/storage/orm/log_writer.go new file mode 100644 index 000000000..975ed483e --- /dev/null +++ b/pkg/storage/orm/log_writer.go @@ -0,0 +1,22 @@ +package orm + +import ( + "fmt" + + "github.com/go-eagle/eagle/pkg/log" + "gorm.io/gorm/logger" +) + +type LoggerWriter struct { + log log.Logger +} + +func NewLogWriter(log log.Logger) logger.Writer { + return &LoggerWriter{ + log: log, + } +} + +func (l *LoggerWriter) Printf(s string, v ...interface{}) { + l.log.Info(fmt.Sprintf(s, v...)) +} diff --git a/pkg/storage/orm/orm.go b/pkg/storage/orm/orm.go index 3d3539c0e..ac1988130 100644 --- a/pkg/storage/orm/orm.go +++ b/pkg/storage/orm/orm.go @@ -35,6 +35,8 @@ var ( DBMap = make(map[string]*gorm.DB) // DBLock database locker DBLock sync.Mutex + // logWriter log writer + LogWriter logger.Writer ) // Config database config @@ -239,9 +241,17 @@ func gormConfig(c *Config) *gorm.Config { } // 只打印慢查询 if c.SlowThreshold > 0 { + var writer logger.Writer + //将标准输出作为Writer + writer = log.New(os.Stdout, "\r\n", log.LstdFlags) + // use custom logger + if LogWriter == nil { + writer = LogWriter + } + + // new logger with writer config.Logger = logger.New( - //将标准输出作为Writer - log.New(os.Stdout, "\r\n", log.LstdFlags), + writer, logger.Config{ //设定慢查询时间阈值 SlowThreshold: c.SlowThreshold, // nolint: golint From 6d4b7deac63f870b0aac8b8761b3ee26e537f311 Mon Sep 17 00:00:00 2001 From: qloog Date: Sun, 20 Oct 2024 17:15:05 +0800 Subject: [PATCH 2/4] feat: fix check cond --- pkg/storage/orm/orm.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/storage/orm/orm.go b/pkg/storage/orm/orm.go index ac1988130..0b8826e6c 100644 --- a/pkg/storage/orm/orm.go +++ b/pkg/storage/orm/orm.go @@ -245,7 +245,7 @@ func gormConfig(c *Config) *gorm.Config { //将标准输出作为Writer writer = log.New(os.Stdout, "\r\n", log.LstdFlags) // use custom logger - if LogWriter == nil { + if LogWriter != nil { writer = LogWriter } From 0a1f7a28cb1c039f750277940be93c0ddb39f214 Mon Sep 17 00:00:00 2001 From: qloog Date: Sun, 20 Oct 2024 19:09:08 +0800 Subject: [PATCH 3/4] feat: add New func for creating a custom logger --- pkg/log/logger.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/pkg/log/logger.go b/pkg/log/logger.go index 4d5c18d8b..215f8faf3 100644 --- a/pkg/log/logger.go +++ b/pkg/log/logger.go @@ -49,7 +49,7 @@ func loadConf() (ret *Config, err error) { return &cfg, nil } -// Init init log +// Init init log and return a global logger func Init(opts ...Option) Logger { var err error cfg, err := loadConf() @@ -72,6 +72,22 @@ func Init(opts ...Option) Logger { return log } +// New create a customer logger +func New(opts ...Option) Logger { + var err error + cfg, err := loadConf() + if err != nil { + panic(fmt.Sprintf("log: New load logger conf err: %v", err)) + } + + l, err := newLoggerWithCallerSkip(cfg, 1, opts...) + if err != nil { + _ = fmt.Errorf("log: New newLoggerWithCallerSkip err: %v", err) + } + + return l +} + // GetLogger return a log func GetLogger() Logger { return log From 4d7344422cf8fcbd17be601ef5e9817560c0db85 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 28 Oct 2024 21:50:16 +0800 Subject: [PATCH 4/4] docs: update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8722897f4..0db8c386d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - feat: support async flush log to disk - feat: add re-entry and automatic renewal for redis lock +- feat: add gorm logger - chore: using gorm offical plguin for tracing and metrics ## v1.9.0