convert repeated error checks into single function in logger (#15387)

This commit is contained in:
jiuker 2022-07-26 08:53:03 +08:00 committed by GitHub
parent 426c902b87
commit 6b4f833a12
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 34 deletions

View file

@ -20,10 +20,8 @@ package logger
import (
"context"
"encoding/hex"
"errors"
"fmt"
"go/build"
"net/http"
"path/filepath"
"reflect"
"runtime"
@ -255,18 +253,9 @@ func LogAlwaysIf(ctx context.Context, err error, errKind ...interface{}) {
// the execution of the server, if it is not an
// ignored error.
func LogIf(ctx context.Context, err error, errKind ...interface{}) {
if err == nil {
if logIgnoreError(err) {
return
}
if errors.Is(err, context.Canceled) {
return
}
if err.Error() == http.ErrServerClosed.Error() || err.Error() == "disk not found" {
return
}
logIf(ctx, err, errKind...)
}

View file

@ -19,8 +19,6 @@ package logger
import (
"context"
"errors"
"net/http"
"sync"
"time"
)
@ -98,34 +96,16 @@ var logOnce = newLogOnceType()
// id is a unique identifier for related log messages, refer to cmd/notification.go
// on how it is used.
func LogOnceIf(ctx context.Context, err error, id interface{}, errKind ...interface{}) {
if err == nil {
if logIgnoreError(err) {
return
}
if errors.Is(err, context.Canceled) {
return
}
if err.Error() == http.ErrServerClosed.Error() || err.Error() == "disk not found" {
return
}
logOnce.logOnceIf(ctx, err, id, errKind...)
}
// LogOnceConsoleIf - similar to LogOnceIf but exclusively only logs to console target.
func LogOnceConsoleIf(ctx context.Context, err error, id interface{}, errKind ...interface{}) {
if err == nil {
if logIgnoreError(err) {
return
}
if errors.Is(err, context.Canceled) {
return
}
if err.Error() == http.ErrServerClosed.Error() || err.Error() == "disk not found" {
return
}
logOnce.logOnceConsoleIf(ctx, err, id, errKind...)
}

View file

@ -18,7 +18,10 @@
package logger
import (
"context"
"errors"
"fmt"
"net/http"
"regexp"
"runtime"
@ -59,3 +62,8 @@ func ansiRestoreAttributes() {
ansiEscape("8")
}
}
// logIgnoreError if true,the error will ignore.
func logIgnoreError(err error) bool {
return err == nil || errors.Is(err, context.Canceled) || errors.Is(err, http.ErrServerClosed) || err.Error() == "disk not found"
}