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

View File

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

View File

@ -18,7 +18,10 @@
package logger package logger
import ( import (
"context"
"errors"
"fmt" "fmt"
"net/http"
"regexp" "regexp"
"runtime" "runtime"
@ -59,3 +62,8 @@ func ansiRestoreAttributes() {
ansiEscape("8") 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"
}