From 04115485c083e3d6a032409da85d3965a6ddeaec Mon Sep 17 00:00:00 2001 From: Steven Soroka Date: Fri, 22 Apr 2022 15:47:53 -0400 Subject: [PATCH 1/2] fix: log trace should not log sql values --- callbacks.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/callbacks.go b/callbacks.go index f344649ea..41c18e8c3 100644 --- a/callbacks.go +++ b/callbacks.go @@ -132,7 +132,7 @@ 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 + return stmt.SQL.String(), db.RowsAffected }, db.Error) } From 7ba745c3f520ceaa229b620e6d9a6d95d88b9654 Mon Sep 17 00:00:00 2001 From: Steven Soroka Date: Fri, 19 Aug 2022 15:07:40 -0400 Subject: [PATCH 2/2] expose ParameterizedQueries config option to remove values from sql logs --- callbacks.go | 9 +++++++-- finisher_api.go | 2 +- logger/logger.go | 13 +++++++------ 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/callbacks.go b/callbacks.go index 41c18e8c3..3c394a09c 100644 --- a/callbacks.go +++ b/callbacks.go @@ -8,6 +8,7 @@ import ( "sort" "time" + "gorm.io/gorm/logger" "gorm.io/gorm/schema" "gorm.io/gorm/utils" ) @@ -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 stmt.SQL.String(), 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) } diff --git a/finisher_api.go b/finisher_api.go index 0bd8f7d99..63a2e1f0c 100644 --- a/finisher_api.go +++ b/finisher_api.go @@ -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 diff --git a/logger/logger.go b/logger/logger.go index 2ffd28d5a..6d3b4a2ac 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -55,6 +55,7 @@ type Config struct { SlowThreshold time.Duration Colorful bool IgnoreRecordNotFoundError bool + ParameterizedQueries bool LogLevel LogLevel } @@ -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 ( @@ -149,7 +150,7 @@ 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 } @@ -157,14 +158,14 @@ func (l logger) Trace(ctx context.Context, begin time.Time, fc func() (string, i 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) @@ -172,7 +173,7 @@ func (l logger) Trace(ctx context.Context, begin time.Time, fc func() (string, i 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 { @@ -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