Skip to content
This repository has been archived by the owner on Nov 24, 2023. It is now read-only.

Commit

Permalink
loader/syncer: fix bug in #355 (#381)
Browse files Browse the repository at this point in the history
* fix situations when there is extra msg in error.Error

* add deal for zap.Error and add UT
  • Loading branch information
lichunzhu authored Nov 29, 2019
1 parent ffd159f commit 7f1e8e2
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
14 changes: 12 additions & 2 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ package log
import (
"context"
"fmt"
"strings"

"github.com/pingcap/errors"
pclog "github.com/pingcap/log"
"github.com/pingcap/tidb/util/logutil"
"go.uber.org/zap"
Expand Down Expand Up @@ -76,8 +78,16 @@ func (l Logger) WithFields(fields ...zap.Field) Logger {
// ErrorFilterContextCanceled wraps Logger.Error() and will filter error log when error is context.Canceled
func (l Logger) ErrorFilterContextCanceled(msg string, fields ...zap.Field) {
for _, field := range fields {
if field.Key == "error" && field.String == context.Canceled.Error() {
return
switch field.Type {
case zapcore.StringType:
if field.Key == "error" && strings.Contains(field.String, context.Canceled.Error()) {
return
}
case zapcore.ErrorType:
err, ok := field.Interface.(error)
if ok && errors.Cause(err) == context.Canceled {
return
}
}
}
l.Logger.Error(msg, fields...)
Expand Down
67 changes: 67 additions & 0 deletions pkg/log/log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package log

import (
"context"
"testing"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest"

. "github.com/pingcap/check"
"github.com/pingcap/errors"
)

func TestLog(t *testing.T) {
TestingT(t)
}

type testLogSuite struct{}

var _ = Suite(&testLogSuite{})

func (s *testLogSuite) TestTestLogger(c *C) {
logger, buffer := makeTestLogger()
logger.Warn("the message", zap.Int("number", 123456), zap.Ints("array", []int{7, 8, 9}))
c.Assert(
buffer.Stripped(), Equals,
`{"$lvl":"WARN","$msg":"the message","number":123456,"array":[7,8,9]}`,
)
buffer.Reset()
logger.ErrorFilterContextCanceled("the message", zap.Int("number", 123456),
zap.Ints("array", []int{7, 8, 9}), zap.Error(context.Canceled))
c.Assert(buffer.Stripped(), Equals, "")
buffer.Reset()
logger.ErrorFilterContextCanceled("the message", zap.Int("number", 123456),
zap.Ints("array", []int{7, 8, 9}), ShortError(errors.Annotate(context.Canceled, "extra info")))
c.Assert(buffer.Stripped(), Equals, "")
}

// makeTestLogger creates a Logger instance which produces JSON logs.
func makeTestLogger() (Logger, *zaptest.Buffer) {
buffer := new(zaptest.Buffer)
logger := zap.New(zapcore.NewCore(
zapcore.NewJSONEncoder(zapcore.EncoderConfig{
LevelKey: "$lvl",
MessageKey: "$msg",
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
}),
buffer,
zap.DebugLevel,
))
return Logger{Logger: logger}, buffer
}

0 comments on commit 7f1e8e2

Please sign in to comment.