Add IgnoreRecordNotFoundError option for logger

This commit is contained in:
Jinzhu 2021-04-09 10:20:36 +08:00
parent 673053f56a
commit f3bdfa8261
2 changed files with 11 additions and 5 deletions

View File

@ -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

View File

@ -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"
@ -45,6 +48,7 @@ type Writer interface {
type Config struct {
SlowThreshold time.Duration
Colorful bool
IgnoreRecordNotFoundError bool
LogLevel LogLevel
}
@ -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)