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

fix(logger): Fixed fc is nil #6676

Closed
wants to merge 1 commit into from
Closed

Conversation

flc1125
Copy link
Contributor

@flc1125 flc1125 commented Nov 7, 2023

  • Do only one thing
  • Non breaking API changes
  • Tested

Copy link

@shedyfreak shedyfreak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lftm, although I would suggest
l.Printf(l.traceWarnStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, "-", "-")
before returning

@flc1125
Copy link
Contributor Author

flc1125 commented Nov 7, 2023

lftm, although I would suggest l.Printf(l.traceWarnStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, "-", "-") before returning

If I return from there, I have code snippets of my switch, almost every one of them, will that cause redundant confusion.

	switch {
	case err != nil && l.LogLevel >= Error && (!errors.Is(err, ErrRecordNotFound) || !l.IgnoreRecordNotFoundError):
		sql, rows := fc()
		// <------ The one
		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()
		// <------ The two
		slowLog := fmt.Sprintf("SLOW SQL >= %v", l.SlowThreshold)
		if rows == -1 {
			l.Printf(l.traceWarnStr, utils.FileWithLineNum(), slowLog, float64(elapsed.Nanoseconds())/1e6, "-", sql)
		} else {
			l.Printf(l.traceWarnStr, utils.FileWithLineNum(), slowLog, float64(elapsed.Nanoseconds())/1e6, rows, sql)
		}
	case l.LogLevel == Info:
		sql, rows := fc()
		// <------ The three
		if rows == -1 {
			l.Printf(l.traceStr, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, "-", sql)
		} else {
			l.Printf(l.traceStr, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, rows, sql)
		}
	}

@jinzhu
Copy link
Member

jinzhu commented Nov 9, 2023

Can you let me know why this is needed? in which cases the fc will be nil when using GORM.

@flc1125
Copy link
Contributor Author

flc1125 commented Nov 9, 2023

Can you let me know why this is needed? in which cases the fc will be nil when using GORM.

I was writing unit tests when I discovered the problem and fixed it together.

@jinzhu
Copy link
Member

jinzhu commented Nov 9, 2023

If we can't reproduce this when using GORM's API directly, I think it is safe to keep it now to avoid unnecessary condition check.

Thank you.

@jinzhu jinzhu closed this Nov 9, 2023
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.

3 participants