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

encountering the error when connecting with database by gorm.Open should not print a error log #6260

Closed
pplmx opened this issue Apr 23, 2023 · 6 comments
Assignees
Labels
type:question general questions

Comments

@pplmx
Copy link

pplmx commented Apr 23, 2023

Your Question

When using docker compose start mysql and app, because mysql init need some time, gorm.Open will throw error and print a error log

Throwing a error is okay, I don't think it should print a error log here

The document you expected this should be explained

If checking with the following, the error has been handled before the timeout, so printing error log is not okay here

	// wait for the database to be ready, max timeout 30s
	timeout := 30
	for i := 0; i < timeout; i++ {
		var err error
		db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
		if err == nil {
			break
		}
		time.Sleep(time.Second)
	}
	if db == nil {
		panic("Failed to connect with database")
	}

Expected answer

Remove this printing error log:

# gorm.io/gorm@v1.25.0/gorm.go +209
	if err != nil {
		config.Logger.Error(context.Background(), "failed to initialize database, got error %v", err)
	}
@pplmx pplmx added the type:question general questions label Apr 23, 2023
@pplmx pplmx changed the title encountering the error while gorm.Open should not print a error log encountering the error when connecting with database by gorm.Open should not print a error log Apr 23, 2023
@a631807682
Copy link
Member

How does printing the error log affect you? In fact, gorm will print error logs in many cases, including query errors.

@labulakalia
Copy link
Contributor

you can use custom Logger replace Default Logger in gorm.Config

@pplmx
Copy link
Author

pplmx commented Apr 23, 2023

How does printing the error log affect you? In fact, gorm will print error logs in many cases, including query errors.

Except for the error logging, no any other side effect actually. In my use scenario, I have already handled the error, but it still print the error log, which is invalid personally.

@pplmx
Copy link
Author

pplmx commented Apr 23, 2023

you can use custom Logger replace Default Logger in gorm.Config

Okay, thanks, I will try it later. :)

@a631807682
Copy link
Member

a631807682 commented Apr 23, 2023

User can also control it by logging level https://gorm.io/docs/logger.html#Log-Levels
We're not going to change this behavior, but you can avoid it.

@pplmx
Copy link
Author

pplmx commented Apr 23, 2023

User can also control it by logging level gorm.io/docs/logger.html#Log-Levels We're not going to change this behavior, but you can avoid it.

Thanks. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question general questions
Projects
None yet
Development

No branches or pull requests

4 participants