Skip to content

Commit

Permalink
*:Start swithing to the zap logger (#1422)
Browse files Browse the repository at this point in the history
Signed-off-by: nolouch <nolouch@gmail.com>
  • Loading branch information
nolouch committed Jul 10, 2019
1 parent 5059637 commit 00c6709
Show file tree
Hide file tree
Showing 20 changed files with 240 additions and 90 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ default.pd
.DS_Store
tags
/.retools/
vendor
default*
15 changes: 12 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,11 @@
[[override]]
name = "github.com/BurntSushi/toml"
revision = "3012a1dbe2e4bd1391d42b32f0577cb7bbc7f005"

[[constraint]]
branch = "master"
name = "github.com/pingcap/log"

[[constraint]]
name = "go.uber.org/zap"
version = "1.9.1"
43 changes: 29 additions & 14 deletions cmd/pd-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"syscall"

"github.com/grpc-ecosystem/go-grpc-prometheus"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
log "github.com/pingcap/log"
"github.com/pingcap/pd/pkg/logutil"
"github.com/pingcap/pd/pkg/metricutil"
"github.com/pingcap/pd/server"
"github.com/pingcap/pd/server/api"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"go.uber.org/zap"

// Register schedulers.
_ "github.com/pingcap/pd/server/schedulers"
Expand All @@ -41,22 +41,32 @@ func main() {

if cfg.Version {
server.PrintPDInfo()
os.Exit(0)
exit(0)
}

defer logutil.LogPanic()

switch errors.Cause(err) {
case nil:
case flag.ErrHelp:
os.Exit(0)
exit(0)
default:
log.Fatalf("parse cmd flags error: %s\n", fmt.Sprintf("%+v", err))
log.Fatal("parse cmd flags error", zap.Error(err))
}
// New zap logger
err = cfg.SetupLogger()
if err == nil {
log.ReplaceGlobals(cfg.GetZapLogger(), cfg.GetZapLogProperties())
} else {
log.Fatal("initialize logger error", zap.Error(err))
}
// Flushing any buffered log entries
defer log.Sync()

// The old logger
err = logutil.InitLogger(&cfg.Log)
if err != nil {
log.Fatalf("initialize logger error: %s\n", fmt.Sprintf("%+v", err))
log.Fatal("initialize logger error", zap.Error(err))
}

server.LogPDInfo()
Expand All @@ -72,15 +82,15 @@ func main() {

err = server.PrepareJoinCluster(cfg)
if err != nil {
log.Fatal("join error ", fmt.Sprintf("%+v", err))
log.Fatal("join meet error", zap.Error(err))
}
svr, err := server.CreateServer(cfg, api.NewHandler)
if err != nil {
log.Fatalf("create server failed: %v", fmt.Sprintf("%+v", err))
log.Fatal("create server failed", zap.Error(err))
}

if err = server.InitHTTPClient(svr); err != nil {
log.Fatalf("initial http client for api handler failed: %v", fmt.Sprintf("%+v", err))
log.Fatal("initial http client for api handler failed", zap.Error(err))
}

sc := make(chan os.Signal, 1)
Expand All @@ -98,17 +108,22 @@ func main() {
}()

if err := svr.Run(ctx); err != nil {
log.Fatalf("run server failed: %v", fmt.Sprintf("%+v", err))
log.Fatal("run server failed", zap.Error(err))
}

<-ctx.Done()
log.Infof("Got signal [%d] to exit.", sig)
log.Info("Got signal to exit", zap.String("signal", sig.String()))

svr.Close()
switch sig {
case syscall.SIGTERM:
os.Exit(0)
exit(0)
default:
os.Exit(1)
exit(1)
}
}

func exit(code int) {
log.Sync()
os.Exit(code)
}
5 changes: 3 additions & 2 deletions pkg/logutil/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/coreos/etcd/raft"
"github.com/coreos/pkg/capnslog"
zaplog "github.com/pingcap/log"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc/grpclog"
Expand Down Expand Up @@ -196,7 +197,7 @@ func stringToLogFormatter(format string, disableTimestamp bool) log.Formatter {
}

// InitFileLog initializes file based logging options.
func InitFileLog(cfg *FileLogConfig) error {
func InitFileLog(cfg *zaplog.FileLogConfig) error {
if st, err := os.Stat(cfg.Filename); err == nil {
if st.IsDir() {
return errors.New("can't use directory as log file name")
Expand Down Expand Up @@ -238,7 +239,7 @@ func (lg *wrapLogrus) V(l int) bool {
var once sync.Once

// InitLogger initializes PD's logger.
func InitLogger(cfg *LogConfig) error {
func InitLogger(cfg *zaplog.Config) error {
var err error

once.Do(func() {
Expand Down
8 changes: 7 additions & 1 deletion pkg/logutil/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/coreos/pkg/capnslog"
. "github.com/pingcap/check"
zaplog "github.com/pingcap/log"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -53,7 +54,7 @@ func (s *testLogSuite) TestStringToLogLevel(c *C) {

// TestLogging assure log format and log redirection works.
func (s *testLogSuite) TestLogging(c *C) {
conf := &LogConfig{Level: "warn", File: FileLogConfig{}}
conf := &zaplog.Config{Level: "warn", File: zaplog.FileLogConfig{}}
c.Assert(InitLogger(conf), IsNil)

log.SetOutput(s.buf)
Expand All @@ -76,3 +77,8 @@ func (s *testLogSuite) TestLogging(c *C) {
c.Assert(entry, Matches, logPattern)
c.Assert(strings.Contains(entry, "log_test.go"), IsTrue)
}

func (s *testLogSuite) TestFileLog(c *C) {
c.Assert(InitFileLog(&zaplog.FileLogConfig{Filename: "/tmp"}), NotNil)
c.Assert(InitFileLog(&zaplog.FileLogConfig{Filename: "/tmp/test_file_log", MaxSize: 0}), IsNil)
}
9 changes: 9 additions & 0 deletions server/api/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ import (
"net/http"
"os"
"strings"
"sync"
"testing"
"time"

. "github.com/pingcap/check"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/pdpb"
log "github.com/pingcap/log"
"github.com/pingcap/pd/pkg/testutil"
"github.com/pingcap/pd/server"
"google.golang.org/grpc"
Expand Down Expand Up @@ -73,13 +75,20 @@ func mustNewServer(c *C) (*server.Server, cleanUpFunc) {
return svrs[0], cleanup
}

var zapLogOnce sync.Once

func mustNewCluster(c *C, num int) ([]*server.Config, []*server.Server, cleanUpFunc) {
svrs := make([]*server.Server, 0, num)
cfgs := server.NewTestMultiConfig(num)

ch := make(chan *server.Server, num)
for _, cfg := range cfgs {
go func(cfg *server.Config) {
err := cfg.SetupLogger()
c.Assert(err, IsNil)
zapLogOnce.Do(func() {
log.ReplaceGlobals(cfg.GetZapLogger(), cfg.GetZapLogProperties())
})
s, err := server.CreateServer(cfg, NewHandler)
c.Assert(err, IsNil)
err = s.Run(context.TODO())
Expand Down
33 changes: 30 additions & 3 deletions server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ import (
"github.com/coreos/etcd/embed"
"github.com/coreos/etcd/pkg/transport"
"github.com/coreos/go-semver/semver"
"github.com/pingcap/pd/pkg/logutil"
"github.com/pingcap/log"
"github.com/pingcap/pd/pkg/metricutil"
"github.com/pingcap/pd/pkg/typeutil"
"github.com/pingcap/pd/server/namespace"
"github.com/pkg/errors"
"go.uber.org/zap"
)

// Config is the pd server configuration.
Expand Down Expand Up @@ -61,7 +62,7 @@ type Config struct {
LeaderLease int64 `toml:"lease" json:"lease"`

// Log related config.
Log logutil.LogConfig `toml:"log" json:"log"`
Log log.Config `toml:"log" json:"log"`

// Backward compatibility.
LogFileDeprecated string `toml:"log-file" json:"log-file"`
Expand Down Expand Up @@ -123,6 +124,9 @@ type Config struct {
heartbeatStreamBindInterval typeutil.Duration

leaderPriorityCheckInterval typeutil.Duration

logger *zap.Logger
logProps *log.ZapProperties
}

// NewConfig creates a new config.
Expand Down Expand Up @@ -756,6 +760,27 @@ func ParseUrls(s string) ([]url.URL, error) {
return urls, nil
}

// SetupLogger setup the logger.
func (c *Config) SetupLogger() error {
lg, p, err := log.InitLogger(&c.Log)
if err != nil {
return err
}
c.logger = lg
c.logProps = p
return nil
}

// GetZapLogger gets the created zap logger.
func (c *Config) GetZapLogger() *zap.Logger {
return c.logger
}

// GetZapLogProperties gets properties of the zap logger.
func (c *Config) GetZapLogProperties() *log.ZapProperties {
return c.logProps
}

// generates a configuration for embedded etcd.
func (c *Config) genEmbedEtcdConfig() (*embed.Config, error) {
cfg := embed.NewConfig()
Expand All @@ -780,7 +805,9 @@ func (c *Config) genEmbedEtcdConfig() (*embed.Config, error) {
cfg.PeerTLSInfo.TrustedCAFile = c.Security.CAPath
cfg.PeerTLSInfo.CertFile = c.Security.CertPath
cfg.PeerTLSInfo.KeyFile = c.Security.KeyPath

// TODO: update etcd
// cfg.ZapLoggerBuilder = embed.NewZapCoreLoggerBuilder(c.logger, c.logger.Core(), c.logSyncer)
// cfg.Logger = "zap"
var err error

cfg.LPUrls, err = ParseUrls(c.PeerUrls)
Expand Down
Loading

0 comments on commit 00c6709

Please sign in to comment.