expose ParameterizedQueries config option to remove values from sql logs
This commit is contained in:
parent
04115485c0
commit
7ba745c3f5
@ -8,6 +8,7 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gorm.io/gorm/logger"
|
||||||
"gorm.io/gorm/schema"
|
"gorm.io/gorm/schema"
|
||||||
"gorm.io/gorm/utils"
|
"gorm.io/gorm/utils"
|
||||||
)
|
)
|
||||||
@ -131,8 +132,12 @@ func (p *processor) Execute(db *DB) *DB {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if stmt.SQL.Len() > 0 {
|
if stmt.SQL.Len() > 0 {
|
||||||
db.Logger.Trace(stmt.Context, curTime, func() (string, int64) {
|
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
|
return stmt.SQL.String(), db.RowsAffected
|
||||||
|
} else {
|
||||||
|
return db.Dialector.Explain(stmt.SQL.String(), stmt.Vars...), db.RowsAffected
|
||||||
|
}
|
||||||
}, db.Error)
|
}, db.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -498,7 +498,7 @@ func (db *DB) Scan(dest interface{}) (tx *DB) {
|
|||||||
tx.AddError(rows.Close())
|
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
|
return newLogger.SQL, tx.RowsAffected
|
||||||
}, tx.Error)
|
}, tx.Error)
|
||||||
tx.Logger = currentLogger
|
tx.Logger = currentLogger
|
||||||
|
@ -55,6 +55,7 @@ type Config struct {
|
|||||||
SlowThreshold time.Duration
|
SlowThreshold time.Duration
|
||||||
Colorful bool
|
Colorful bool
|
||||||
IgnoreRecordNotFoundError bool
|
IgnoreRecordNotFoundError bool
|
||||||
|
ParameterizedQueries bool
|
||||||
LogLevel LogLevel
|
LogLevel LogLevel
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,7 +65,7 @@ type Interface interface {
|
|||||||
Info(context.Context, string, ...interface{})
|
Info(context.Context, string, ...interface{})
|
||||||
Warn(context.Context, string, ...interface{})
|
Warn(context.Context, string, ...interface{})
|
||||||
Error(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 (
|
var (
|
||||||
@ -149,7 +150,7 @@ func (l logger) Error(ctx context.Context, msg string, data ...interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Trace print sql message
|
// 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 {
|
if l.LogLevel <= Silent {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -157,14 +158,14 @@ func (l logger) Trace(ctx context.Context, begin time.Time, fc func() (string, i
|
|||||||
elapsed := time.Since(begin)
|
elapsed := time.Since(begin)
|
||||||
switch {
|
switch {
|
||||||
case err != nil && l.LogLevel >= Error && (!errors.Is(err, ErrRecordNotFound) || !l.IgnoreRecordNotFoundError):
|
case err != nil && l.LogLevel >= Error && (!errors.Is(err, ErrRecordNotFound) || !l.IgnoreRecordNotFoundError):
|
||||||
sql, rows := fc()
|
sql, rows := fc(l.Config)
|
||||||
if rows == -1 {
|
if rows == -1 {
|
||||||
l.Printf(l.traceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, "-", sql)
|
l.Printf(l.traceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, "-", sql)
|
||||||
} else {
|
} else {
|
||||||
l.Printf(l.traceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, rows, sql)
|
l.Printf(l.traceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, rows, sql)
|
||||||
}
|
}
|
||||||
case elapsed > l.SlowThreshold && l.SlowThreshold != 0 && l.LogLevel >= Warn:
|
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)
|
slowLog := fmt.Sprintf("SLOW SQL >= %v", l.SlowThreshold)
|
||||||
if rows == -1 {
|
if rows == -1 {
|
||||||
l.Printf(l.traceWarnStr, utils.FileWithLineNum(), slowLog, float64(elapsed.Nanoseconds())/1e6, "-", sql)
|
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)
|
l.Printf(l.traceWarnStr, utils.FileWithLineNum(), slowLog, float64(elapsed.Nanoseconds())/1e6, rows, sql)
|
||||||
}
|
}
|
||||||
case l.LogLevel == Info:
|
case l.LogLevel == Info:
|
||||||
sql, rows := fc()
|
sql, rows := fc(l.Config)
|
||||||
if rows == -1 {
|
if rows == -1 {
|
||||||
l.Printf(l.traceStr, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, "-", sql)
|
l.Printf(l.traceStr, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, "-", sql)
|
||||||
} else {
|
} else {
|
||||||
@ -195,7 +196,7 @@ func (l traceRecorder) New() *traceRecorder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Trace implement logger interface
|
// 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.BeginAt = begin
|
||||||
l.SQL, l.RowsAffected = fc()
|
l.SQL, l.RowsAffected = fc()
|
||||||
l.Err = err
|
l.Err = err
|
||||||
|
Loading…
x
Reference in New Issue
Block a user