diff --git a/engine.go b/engine.go index 0c794512d..8ee53ea58 100644 --- a/engine.go +++ b/engine.go @@ -329,7 +329,7 @@ func (engine *Engine) Ping() error { return session.Ping() } -// logging sql +// logSQL save sql func (engine *Engine) logSQL(sqlStr string, sqlArgs ...interface{}) { if engine.showSQL && !engine.showExecTime { if len(sqlArgs) > 0 { diff --git a/session.go b/session.go index b33955fdc..830719355 100644 --- a/session.go +++ b/session.go @@ -57,6 +57,7 @@ type Session struct { //beforeSQLExec func(string, ...interface{}) lastSQL string lastSQLArgs []interface{} + showSQL bool ctx context.Context sessionType sessionType @@ -72,6 +73,7 @@ func (session *Session) Clone() *Session { func (session *Session) Init() { session.statement.Init() session.statement.Engine = session.engine + session.showSQL = session.engine.showSQL session.isAutoCommit = true session.isCommitedOrRollbacked = false session.isAutoClose = false @@ -226,6 +228,16 @@ func (session *Session) Cascade(trueOrFalse ...bool) *Session { return session } +// MustLogSQL means record SQL or not and don't follow engine's setting +func (session *Session) MustLogSQL(log ...bool) *Session { + if len(log) > 0 { + session.showSQL = log[0] + } else { + session.showSQL = true + } + return session +} + // NoCache ask this session do not retrieve data from cache system and // get data from database directly. func (session *Session) NoCache() *Session { @@ -842,7 +854,17 @@ func (session *Session) slice2Bean(scanResults []interface{}, fields []string, b func (session *Session) saveLastSQL(sql string, args ...interface{}) { session.lastSQL = sql session.lastSQLArgs = args - session.engine.logSQL(sql, args...) + session.logSQL(sql, args...) +} + +func (session *Session) logSQL(sqlStr string, sqlArgs ...interface{}) { + if session.showSQL && !session.engine.showExecTime { + if len(sqlArgs) > 0 { + session.engine.logger.Infof("[SQL] %v %#v", sqlStr, sqlArgs) + } else { + session.engine.logger.Infof("[SQL] %v", sqlStr) + } + } } // LastSQL returns last query information diff --git a/session_raw.go b/session_raw.go index 67648ef13..a92982967 100644 --- a/session_raw.go +++ b/session_raw.go @@ -27,7 +27,9 @@ func (session *Session) queryRows(sqlStr string, args ...interface{}) (*core.Row session.queryPreprocess(&sqlStr, args...) - if session.engine.showSQL { + if session.showSQL { + session.lastSQL = sqlStr + session.lastSQLArgs = args if session.engine.showExecTime { b4ExecTime := time.Now() defer func() {