From f6117b7f3dd21629b8196c376b0284d71672d1c7 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 9 Sep 2020 16:26:11 +0800 Subject: [PATCH] Should not diplay SubQuery SQL log, close #3437 --- logger/logger.go | 14 +++++++++----- statement.go | 3 ++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/logger/logger.go b/logger/logger.go index 0b0a7377..831192fc 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -2,6 +2,7 @@ package logger import ( "context" + "io/ioutil" "log" "os" "time" @@ -54,11 +55,14 @@ type Interface interface { Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) } -var Default = New(log.New(os.Stdout, "\r\n", log.LstdFlags), Config{ - SlowThreshold: 100 * time.Millisecond, - LogLevel: Warn, - Colorful: true, -}) +var ( + Discard = New(log.New(ioutil.Discard, "", log.LstdFlags), Config{}) + Default = New(log.New(os.Stdout, "\r\n", log.LstdFlags), Config{ + SlowThreshold: 100 * time.Millisecond, + LogLevel: Warn, + Colorful: true, + }) +) func New(writer Writer, config Config) Interface { var ( diff --git a/statement.go b/statement.go index e16cf0ff..ee80f8cd 100644 --- a/statement.go +++ b/statement.go @@ -12,6 +12,7 @@ import ( "sync" "gorm.io/gorm/clause" + "gorm.io/gorm/logger" "gorm.io/gorm/schema" "gorm.io/gorm/utils" ) @@ -189,7 +190,7 @@ func (stmt *Statement) AddVar(writer clause.Writer, vars ...interface{}) { writer.WriteString("(NULL)") } case *DB: - subdb := v.Session(&Session{DryRun: true, WithConditions: true}).getInstance() + subdb := v.Session(&Session{Logger: logger.Discard, DryRun: true, WithConditions: true}).getInstance() subdb.Statement.Vars = append(subdb.Statement.Vars, stmt.Vars...) subdb.callbacks.Query().Execute(subdb) writer.WriteString(subdb.Statement.SQL.String())