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)
    }

Comment From: a631807682

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

Comment From: labulakalia

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

Comment From: pplmx

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.

Comment From: pplmx

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

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

Comment From: a631807682

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.

Comment From: pplmx

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. :)