Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add log writer for gorm and add New func for log #155

Merged
merged 4 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion pkg/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
22 changes: 22 additions & 0 deletions pkg/storage/orm/log_writer.go
Original file line number Diff line number Diff line change
@@ -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...))
}
14 changes: 12 additions & 2 deletions pkg/storage/orm/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading