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: log trace should not log sql values #5288

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions callbacks.go
Expand Up @@ -8,6 +8,7 @@ import (
"sort"
"time"

"gorm.io/gorm/logger"
"gorm.io/gorm/schema"
"gorm.io/gorm/utils"
)
Expand Down Expand Up @@ -131,8 +132,12 @@ func (p *processor) Execute(db *DB) *DB {
}

if stmt.SQL.Len() > 0 {
db.Logger.Trace(stmt.Context, curTime, func() (string, int64) {
return db.Dialector.Explain(stmt.SQL.String(), stmt.Vars...), db.RowsAffected
db.Logger.Trace(stmt.Context, curTime, func(config ...logger.Config) (string, int64) {
if len(config) > 0 && config[0].ParameterizedQueries {
return stmt.SQL.String(), db.RowsAffected
} else {
return db.Dialector.Explain(stmt.SQL.String(), stmt.Vars...), db.RowsAffected
}
}, db.Error)
}

Expand Down
2 changes: 1 addition & 1 deletion finisher_api.go
Expand Up @@ -498,7 +498,7 @@ func (db *DB) Scan(dest interface{}) (tx *DB) {
tx.AddError(rows.Close())
}

currentLogger.Trace(tx.Statement.Context, newLogger.BeginAt, func() (string, int64) {
currentLogger.Trace(tx.Statement.Context, newLogger.BeginAt, func(...logger.Config) (string, int64) {
return newLogger.SQL, tx.RowsAffected
}, tx.Error)
tx.Logger = currentLogger
Expand Down
13 changes: 7 additions & 6 deletions logger/logger.go
Expand Up @@ -55,6 +55,7 @@ type Config struct {
SlowThreshold time.Duration
Colorful bool
IgnoreRecordNotFoundError bool
ParameterizedQueries bool
LogLevel LogLevel
}

Expand All @@ -64,7 +65,7 @@ type Interface interface {
Info(context.Context, string, ...interface{})
Warn(context.Context, string, ...interface{})
Error(context.Context, string, ...interface{})
Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error)
Trace(ctx context.Context, begin time.Time, fc func(config ...Config) (sql string, rowsAffected int64), err error)
}

var (
Expand Down Expand Up @@ -149,30 +150,30 @@ func (l logger) Error(ctx context.Context, msg string, data ...interface{}) {
}

// Trace print sql message
func (l logger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
func (l logger) Trace(ctx context.Context, begin time.Time, fc func(config ...Config) (string, int64), err error) {
if l.LogLevel <= Silent {
return
}

elapsed := time.Since(begin)
switch {
case err != nil && l.LogLevel >= Error && (!errors.Is(err, ErrRecordNotFound) || !l.IgnoreRecordNotFoundError):
sql, rows := fc()
sql, rows := fc(l.Config)
if rows == -1 {
l.Printf(l.traceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, "-", sql)
} else {
l.Printf(l.traceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, rows, sql)
}
case elapsed > l.SlowThreshold && l.SlowThreshold != 0 && l.LogLevel >= Warn:
sql, rows := fc()
sql, rows := fc(l.Config)
slowLog := fmt.Sprintf("SLOW SQL >= %v", l.SlowThreshold)
if rows == -1 {
l.Printf(l.traceWarnStr, utils.FileWithLineNum(), slowLog, float64(elapsed.Nanoseconds())/1e6, "-", sql)
} else {
l.Printf(l.traceWarnStr, utils.FileWithLineNum(), slowLog, float64(elapsed.Nanoseconds())/1e6, rows, sql)
}
case l.LogLevel == Info:
sql, rows := fc()
sql, rows := fc(l.Config)
if rows == -1 {
l.Printf(l.traceStr, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, "-", sql)
} else {
Expand All @@ -195,7 +196,7 @@ func (l traceRecorder) New() *traceRecorder {
}

// Trace implement logger interface
func (l *traceRecorder) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
func (l *traceRecorder) Trace(ctx context.Context, begin time.Time, fc func(config ...Config) (string, int64), err error) {
l.BeginAt = begin
l.SQL, l.RowsAffected = fc()
l.Err = err
Expand Down