forked from mirror/gorm
Add IgnoreRecordNotFoundError option for logger
This commit is contained in:
parent
673053f56a
commit
f3bdfa8261
|
@ -2,11 +2,13 @@ package gorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
|
"gorm.io/gorm/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// ErrRecordNotFound record not found error
|
// 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 invalid transaction when you are trying to `Commit` or `Rollback`
|
||||||
ErrInvalidTransaction = errors.New("no valid transaction")
|
ErrInvalidTransaction = errors.New("no valid transaction")
|
||||||
// ErrNotImplemented not implemented
|
// ErrNotImplemented not implemented
|
||||||
|
|
|
@ -2,6 +2,7 @@ package logger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
@ -11,6 +12,8 @@ import (
|
||||||
"gorm.io/gorm/utils"
|
"gorm.io/gorm/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ErrRecordNotFound = errors.New("record not found")
|
||||||
|
|
||||||
// Colors
|
// Colors
|
||||||
const (
|
const (
|
||||||
Reset = "\033[0m"
|
Reset = "\033[0m"
|
||||||
|
@ -43,9 +46,10 @@ type Writer interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
SlowThreshold time.Duration
|
SlowThreshold time.Duration
|
||||||
Colorful bool
|
Colorful bool
|
||||||
LogLevel LogLevel
|
IgnoreRecordNotFoundError bool
|
||||||
|
LogLevel LogLevel
|
||||||
}
|
}
|
||||||
|
|
||||||
// Interface logger interface
|
// 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 {
|
if l.LogLevel > Silent {
|
||||||
elapsed := time.Since(begin)
|
elapsed := time.Since(begin)
|
||||||
switch {
|
switch {
|
||||||
case err != nil && l.LogLevel >= Error:
|
case err != nil && l.LogLevel >= Error && (!errors.Is(err, ErrRecordNotFound) || !l.IgnoreRecordNotFoundError):
|
||||||
sql, rows := fc()
|
sql, rows := fc()
|
||||||
if rows == -1 {
|
if rows == -1 {
|
||||||
l.Printf(l.traceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, "-", sql)
|
l.Printf(l.traceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, "-", sql)
|
||||||
|
|
Loading…
Reference in New Issue