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

修复日志追踪traceRecorder初始化函数New没有赋予默认值导致的bug #3657

Merged
merged 1 commit into from
Oct 22, 2020
Merged

Conversation

qifengzhang007
Copy link
Contributor

@qifengzhang007 qifengzhang007 commented Oct 22, 2020

bug所在文件:logger/logger.go, 167 行代码块
相关函数:New 没有初始化 BeginAt参数, 默认值 0001-00-00 00:00:00

type traceRecorder struct {
	Interface
	BeginAt      time.Time
	SQL          string
	RowsAffected int64
	Err          error
}

func (l traceRecorder) New() *traceRecorder {
	return &traceRecorder{Interface: l.Interface}
}

func (l *traceRecorder) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
	l.BeginAt = begin
	l.SQL, l.RowsAffected = fc()
	l.Err = err
}

触发bug的条件:
相关文件:finisher_api.go ,358行代码段,
调用了函数: logger.Recorder.New()、 newLogger.BeginAt

// Scan scan value to a struct
func (db *DB) Scan(dest interface{}) (tx *DB) {

	currentLogger, newLogger := db.Logger, logger.Recorder.New()
	tx = db.getInstance()
	tx.Logger = newLogger
	if rows, err := tx.Rows(); err != nil {
		tx.AddError(err)
	} else {
		defer rows.Close()
		if rows.Next() {
			tx.ScanRows(rows, dest)
		}
	}

	currentLogger.Trace(tx.Statement.Context, newLogger.BeginAt, func() (string, int64) {
		return newLogger.SQL, tx.RowsAffected
	}, tx.Error)
	tx.Logger = currentLogger
	return
}

如果sql执行时长超过默认的 100*time.Million, 就会触发 l.Printf(l.traceWarnStr ) 日志输出,日志输出时,显示的sql耗时为无限大

@qifengzhang007 qifengzhang007 changed the title 修复日志追踪函数trace初始化函数New没有赋予默认值导致的bug 修复日志追踪traceRecorder初始化函数New没有赋予默认值导致的bug Oct 22, 2020
@jinzhu jinzhu merged commit 6d90d09 into go-gorm:master Oct 22, 2020
@jinzhu
Copy link
Member

jinzhu commented Oct 22, 2020

thank you for your PR.

cgxxv pushed a commit to cgxxv/gorm that referenced this pull request Mar 25, 2022
…001-0:0:0导致追踪日志显示的sql耗时无限大. (go-gorm#3657)

Co-authored-by: 张奇峰 <10515935zwj>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants