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

fix: db log level mapping to logrus #327

Merged
merged 1 commit into from
Nov 6, 2023
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
15 changes: 1 addition & 14 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,19 @@ import (
"github.com/sirupsen/logrus"
gormpostgres "gorm.io/driver/postgres"
"gorm.io/gorm"
glogger "gorm.io/gorm/logger"
)

var pool *pgxpool.Pool

var DefaultQueryTimeout = 30 * time.Second

func DefaultGormConfig() *gorm.Config {
var gloggerLevel glogger.LogLevel
switch LogLevel {
case "warn":
gloggerLevel = glogger.Warn
case "error":
gloggerLevel = glogger.Error
case "info":
gloggerLevel = glogger.Info
default:
gloggerLevel = glogger.Silent
}

return &gorm.Config{
FullSaveAssociations: true,
NowFunc: func() time.Time {
return time.Now().UTC()
},
Logger: NewGormLogger().LogMode(gloggerLevel),
Logger: NewGormLogger(LogLevel),
}
}

Expand Down
33 changes: 18 additions & 15 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
var LogLevel string

func BindFlags(flags *pflag.FlagSet) {
flags.StringVar(&LogLevel, "db-log-level", "error", "Set gorm logging level. error, warn & info")
flags.StringVar(&LogLevel, "db-log-level", "error", "Set gorm logging level. trace, debug & info")
}

type gormLogger struct {
Expand All @@ -25,31 +25,34 @@ type gormLogger struct {
IgnoreRecordNotFoundError bool
}

func NewGormLogger() gLogger.Interface {
l := logrus.StandardLogger()
func NewGormLogger(level string) gLogger.Interface {
l := logrus.New()
l.SetFormatter(&logrus.TextFormatter{
ForceColors: true,
DisableQuote: true,
})

currentGormLogger := logger.NewLogrusLogger(l)

switch LogLevel {
case "trace":
currentGormLogger.SetLogLevel(2)
case "debug":
currentGormLogger.SetLogLevel(1)
default:
currentGormLogger.SetLogLevel(0)
}

return &gormLogger{
SlowThreshold: time.Second,
logger: logger.NewLogrusLogger(l),
logger: currentGormLogger,
}
}

// Pass the log level directly to NewGormLogger
func (t *gormLogger) LogMode(level gLogger.LogLevel) gLogger.Interface {
switch level {
case gLogger.Silent:
t.logger.SetLogLevel(int(logrus.FatalLevel))
case gLogger.Error:
t.logger.SetLogLevel(int(logrus.ErrorLevel))
case gLogger.Warn:
t.logger.SetLogLevel(int(logrus.WarnLevel))
default:
t.logger.SetLogLevel(int(logrus.InfoLevel))
}

// not applicable since the mapping of gorm's loglevel to common's logger's log level
// doesn't work out well.
return t
}

Expand Down
Loading