Skip to content

Commit

Permalink
*: log format for logutil, server/core (tikv#1438)
Browse files Browse the repository at this point in the history
* *: log format for logutil,server/core
  • Loading branch information
nolouch committed Jul 10, 2019
1 parent d08afe7 commit 4de76bc
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 152 deletions.
2 changes: 1 addition & 1 deletion pkg/etcdutil/etcdutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/etcdserver"
"github.com/coreos/etcd/pkg/types"
log "github.com/pingcap/log"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

const (
Expand Down
22 changes: 20 additions & 2 deletions pkg/logutil/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"os"
"path"
"runtime"
"runtime/debug"
"strings"
"sync"

Expand All @@ -28,6 +27,8 @@ import (
zaplog "github.com/pingcap/log"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/grpc/grpclog"
lumberjack "gopkg.in/natefinch/lumberjack.v2"
)
Expand Down Expand Up @@ -145,6 +146,23 @@ func StringToLogLevel(level string) log.Level {
return defaultLogLevel
}

// StringToZapLogLevel translates log level string to log level.
func StringToZapLogLevel(level string) zapcore.Level {
switch strings.ToLower(level) {
case "fatal":
return zapcore.FatalLevel
case "error":
return zapcore.ErrorLevel
case "warn", "warning":
return zapcore.WarnLevel
case "debug":
return zapcore.DebugLevel
case "info":
return zapcore.InfoLevel
}
return zapcore.InfoLevel
}

// textFormatter is for compatibility with ngaut/log
type textFormatter struct {
DisableTimestamp bool
Expand Down Expand Up @@ -272,6 +290,6 @@ func InitLogger(cfg *zaplog.Config) error {
// Commonly used with a `defer`.
func LogPanic() {
if e := recover(); e != nil {
log.Fatalf("panic: %v, stack: %s", e, string(debug.Stack()))
zaplog.Fatal("panic", zap.Reflect("recover", e))
}
}
11 changes: 11 additions & 0 deletions pkg/logutil/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
. "github.com/pingcap/check"
zaplog "github.com/pingcap/log"
log "github.com/sirupsen/logrus"
"go.uber.org/zap/zapcore"
)

const (
Expand Down Expand Up @@ -52,6 +53,16 @@ func (s *testLogSuite) TestStringToLogLevel(c *C) {
c.Assert(StringToLogLevel("whatever"), Equals, log.InfoLevel)
}

func (s *testLogSuite) TestStringToZapLogLevel(c *C) {
c.Assert(StringToZapLogLevel("fatal"), Equals, zapcore.FatalLevel)
c.Assert(StringToZapLogLevel("ERROR"), Equals, zapcore.ErrorLevel)
c.Assert(StringToZapLogLevel("warn"), Equals, zapcore.WarnLevel)
c.Assert(StringToZapLogLevel("warning"), Equals, zapcore.WarnLevel)
c.Assert(StringToZapLogLevel("debug"), Equals, zapcore.DebugLevel)
c.Assert(StringToZapLogLevel("info"), Equals, zapcore.InfoLevel)
c.Assert(StringToZapLogLevel("whatever"), Equals, zapcore.InfoLevel)
}

// TestLogging assure log format and log redirection works.
func (s *testLogSuite) TestLogging(c *C) {
conf := &zaplog.Config{Level: "warn", File: zaplog.FileLogConfig{}}
Expand Down
4 changes: 2 additions & 2 deletions server/api/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
"io/ioutil"
"net/http"

log "github.com/pingcap/log"
"github.com/pingcap/pd/pkg/logutil"
"github.com/pingcap/pd/server"
log "github.com/sirupsen/logrus"
"github.com/unrolled/render"
)

Expand Down Expand Up @@ -51,7 +51,7 @@ func (h *logHandler) Handle(w http.ResponseWriter, r *http.Request) {
}

h.svr.SetLogLevel(level)
log.SetLevel(logutil.StringToLogLevel(level))
log.SetLevel(logutil.StringToZapLogLevel(level))

h.rd.JSON(w, http.StatusOK, nil)
}
5 changes: 3 additions & 2 deletions server/cache/ttl.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import (
"sync"
"time"

log "github.com/sirupsen/logrus"
log "github.com/pingcap/log"
"go.uber.org/zap"
)

type ttlCacheItem struct {
Expand Down Expand Up @@ -124,7 +125,7 @@ func (c *TTL) doGC() {
}
c.Unlock()

log.Debugf("GC %d items", count)
log.Debug("TTL GC items", zap.Int("count", count))
}
}

Expand Down
8 changes: 6 additions & 2 deletions server/core/region_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import (

"github.com/google/btree"
"github.com/pingcap/kvproto/pkg/metapb"
log "github.com/sirupsen/logrus"
log "github.com/pingcap/log"
"go.uber.org/zap"
)

var _ btree.Item = &regionItem{}
Expand Down Expand Up @@ -89,7 +90,10 @@ func (t *regionTree) getOverlaps(region *metapb.Region) []*metapb.Region {
func (t *regionTree) update(region *metapb.Region) []*metapb.Region {
overlaps := t.getOverlaps(region)
for _, item := range overlaps {
log.Debugf("[region %d] delete region %v, cause overlapping with region %v", item.GetId(), HexRegionMeta(item), HexRegionMeta(region))
log.Debug("overlapping region",
zap.Uint64("region-id", item.GetId()),
zap.Reflect("delete-region", HexRegionMeta(item)),
zap.Reflect("update-region", HexRegionMeta(region)))
t.tree.Delete(&regionItem{item})
}

Expand Down
8 changes: 5 additions & 3 deletions server/core/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import (
"github.com/gogo/protobuf/proto"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pingcap/pd/pkg/error_code"
log "github.com/sirupsen/logrus"
log "github.com/pingcap/log"
errcode "github.com/pingcap/pd/pkg/error_code"
"go.uber.org/zap"
)

// StoreInfo contains information about a store.
Expand Down Expand Up @@ -387,7 +388,8 @@ func (s *StoresInfo) BlockStore(storeID uint64) errcode.ErrorCode {
func (s *StoresInfo) UnblockStore(storeID uint64) {
store, ok := s.stores[storeID]
if !ok {
log.Fatalf("store %d is unblocked, but it is not found", storeID)
log.Fatal("store is unblocked, but it is not found",
zap.Uint64("store-id", storeID))
}
store.Unblock()
}
Expand Down
2 changes: 2 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,8 @@ func (s *Server) GetMemberLeaderPriority(id uint64) (int, error) {
// SetLogLevel sets log level.
func (s *Server) SetLogLevel(level string) {
s.cfg.Log.Level = level
log.SetLevel(logutil.StringToZapLogLevel(level))
log.Warn("log level changed", zap.String("level", log.GetLevel().String()))
}

var healthURL = "/pd/ping"
Expand Down
140 changes: 0 additions & 140 deletions tools/pd-simulator/simulator/cases/import_data.go

This file was deleted.

0 comments on commit 4de76bc

Please sign in to comment.