Skip to content
Open
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
71 changes: 10 additions & 61 deletions gorm/logger.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
package duty

import (
"context"
"errors"
"log"
"os"
"time"

"github.com/flanksource/commons/logger"
gLogger "gorm.io/gorm/logger"
)

const Debug = "debug"
const Trace = "trace"

type gormLogger struct {
logger logger.Logger
SlowThreshold time.Duration
IgnoreRecordNotFoundError bool
}

func NewGormLogger(level string) gLogger.Interface {
if level == Trace {
return gLogger.New(
Expand Down Expand Up @@ -47,56 +38,14 @@ func NewGormLogger(level string) gLogger.Interface {
)
}

currentGormLogger := logger.StandardLogger().Named("db")

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

return &gormLogger{
SlowThreshold: time.Second,
logger: currentGormLogger,
}
}

// Pass the log level directly to NewGormLogger
func (t *gormLogger) LogMode(level gLogger.LogLevel) gLogger.Interface {
// not applicable since the mapping of gorm's loglevel to common's logger's log level
// doesn't work out well.
return t
}

func (l *gormLogger) Info(ctx context.Context, msg string, data ...interface{}) {
l.logger.Infof(msg, data)
}

func (l *gormLogger) Warn(ctx context.Context, msg string, data ...interface{}) {
l.logger.Warnf(msg, data)
}

func (l *gormLogger) Error(ctx context.Context, msg string, data ...interface{}) {
l.logger.Errorf(msg, data)
}

func (l *gormLogger) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) {
if !l.logger.IsTraceEnabled() {
return
}

elapsed := time.Since(begin)
sql, rows := fc()

switch {
case err != nil && (!errors.Is(err, gLogger.ErrRecordNotFound) || !l.IgnoreRecordNotFoundError):
l.logger.WithValues("elapsed", elapsed).WithValues("rows", rows).Errorf(sql)
case elapsed > l.SlowThreshold && l.SlowThreshold != 0:
l.logger.WithValues("elapsed", elapsed).WithValues("slow SQL", l.SlowThreshold).WithValues("rows", rows).Warnf(sql)
default:
l.logger.WithValues("elapsed", elapsed).WithValues("rows", rows).Infof(sql)
}
return gLogger.New(
log.New(os.Stdout, "\r\n", log.Ldate|log.Ltime|log.Lshortfile), // io writer
gLogger.Config{
SlowThreshold: time.Second, // Slow SQL threshold
LogLevel: gLogger.Silent, // Log level
IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
ParameterizedQueries: false, // Don't include params in the SQL log
Colorful: true, // Disable color
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Colorful setting is set to true but the comment indicates it disables color. These should be aligned - either set Colorful: false to disable colors, or update the comment to "Enable color" to match the current setting.

Spotted by Graphite Reviewer

Is this helpful? React 👍 or 👎 to let us know.

},
)
}