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" | ||||||
| @ -45,6 +48,7 @@ type Writer interface { | |||||||
| type Config struct { | type Config struct { | ||||||
| 	SlowThreshold             time.Duration | 	SlowThreshold             time.Duration | ||||||
| 	Colorful                  bool | 	Colorful                  bool | ||||||
|  | 	IgnoreRecordNotFoundError bool | ||||||
| 	LogLevel                  LogLevel | 	LogLevel                  LogLevel | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -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…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Jinzhu
						Jinzhu