diff --git a/errors.go b/errors.go index 5f464d2b..3126b8e7 100644 --- a/errors.go +++ b/errors.go @@ -2,11 +2,13 @@ package gorm import ( "errors" + + "gorm.io/gorm/logger" ) var ( // ErrRecordNotFound record not found error - ErrRecordNotFound = errors.New("record not found") + ErrRecordNotFound = logger.ErrRecordNotFound // ErrInvalidTransaction invalid transaction when you are trying to `Commit` or `Rollback` ErrInvalidTransaction = errors.New("no valid transaction") // ErrNotImplemented not implemented diff --git a/logger/logger.go b/logger/logger.go index cd6bf57f..f14748c1 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -2,6 +2,7 @@ package logger import ( "context" + "errors" "fmt" "io/ioutil" "log" @@ -11,6 +12,8 @@ import ( "gorm.io/gorm/utils" ) +var ErrRecordNotFound = errors.New("record not found") + // Colors const ( Reset = "\033[0m" @@ -43,9 +46,10 @@ type Writer interface { } type Config struct { - SlowThreshold time.Duration - Colorful bool - LogLevel LogLevel + SlowThreshold time.Duration + Colorful bool + IgnoreRecordNotFoundError bool + LogLevel LogLevel } // Interface logger interface @@ -138,7 +142,7 @@ func (l logger) Trace(ctx context.Context, begin time.Time, fc func() (string, i if l.LogLevel > Silent { elapsed := time.Since(begin) switch { - case err != nil && l.LogLevel >= Error: + case err != nil && l.LogLevel >= Error && (!errors.Is(err, ErrRecordNotFound) || !l.IgnoreRecordNotFoundError): sql, rows := fc() if rows == -1 { l.Printf(l.traceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, "-", sql)