Skip to content

Commit

Permalink
modify sarama logging logic
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoxinyu committed May 24, 2022
1 parent 058425f commit f574f2b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
2 changes: 0 additions & 2 deletions cdc/sink/mq/producer/kafka/kafka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package kafka

import (
"context"
"fmt"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -341,7 +340,6 @@ func TestAdjustConfigMinInsyncReplicas(t *testing.T) {
// when the topic does exist.
saramaConfig, err = NewSaramaConfig(context.Background(), config)
require.Nil(t, err)
fmt.Println(config.PartitionNum)

// topic exist, but `min.insync.replicas` not found in topic and broker configuration
topicName = "topic-no-config-entry"
Expand Down
7 changes: 7 additions & 0 deletions pkg/cmd/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ package cli
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"

"github.com/Shopify/sarama"
"github.com/chzyer/readline"
"github.com/mattn/go-shellwords"
"github.com/pingcap/tiflow/pkg/cmd/factory"
Expand Down Expand Up @@ -68,6 +71,10 @@ func NewCmdCli() *cobra.Command {
run()
}
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
// discard sarama logging after executing the cli cmd.
sarama.Logger = log.New(ioutil.Discard, "[Sarama] ", log.LstdFlags)
},
}

// Binding the `cli` command flags.
Expand Down
16 changes: 10 additions & 6 deletions pkg/logutil/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,15 @@ func InitLogger(cfg *Config) error {
if err != nil {
return err
}
reset, err := initSaramaLogger(pclogConfig)
defer reset()
if err != nil {
return err

if cfg.File == "" {
reset, err := initSaramaLogger()
defer reset()
if err != nil {
return err
}
}

return nil
}

Expand All @@ -138,10 +142,10 @@ func ZapErrorFilter(err error, filterErrors ...error) zap.Field {
return zap.Error(err)
}

// InitSaramaLogger hacks logger used in sarama lib.
// initSaramaLogger hacks logger used in sarama lib.
// this function use info-level logging for sarama and returns a function
// to restore the original logger later.
func initSaramaLogger(cfg *log.Config) (func(), error) {
func initSaramaLogger() (func(), error) {
conf := &log.Config{Level: "info"}
logger, props, _ := log.InitLogger(conf)
restoreFn := log.ReplaceGlobals(logger, props)
Expand Down

0 comments on commit f574f2b

Please sign in to comment.