custom log sql generation function

custom log sql generation function

change Logger Interface Trace Method

now :

type Interface interface {
         ...
    Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error)
}

to change Trace callback method

type Interface interface {
         ...
    Trace(ctx context.Context, begin time.Time, fc func(config Config) (sql string, rowsAffected int64), err error)
}

and add SqlPrintFunc to Config

type SqlPrintFunc func(db *gorm.DB, statement *gorm.Statement) (sql string, rowsAffected int64)

// Config logger config
type Config struct {
    SlowThreshold             time.Duration
    Colorful                  bool
    IgnoreRecordNotFoundError bool
    LogLevel                  LogLevel
    SqlPrintFunc              SqlPrintFunc // custom log's callback method
}

finally, we can custom sql Gen function

db.Logger.Trace(stmt.Context, curTime, func(config logger.Config) (string, int64) {
    if config.SqlPrintFunc != nil {
        return config.SqlPrintFunc(db, stmt)
    }

    return db.Dialector.Explain(stmt.SQL.String(), stmt.Vars...), db.RowsAffected
}, db.Error)

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking