Skip to content

Commit

Permalink
*: log format for logutil,server/core
Browse files Browse the repository at this point in the history
Signed-off-by: nolouch <[email protected]>
  • Loading branch information
nolouch committed Feb 22, 2019
1 parent 5e81548 commit ce9508b
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 14 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 @@ -273,6 +291,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)
}

func (s *testLogSuite) TestStringToLogFormatter(c *C) {
c.Assert(StringToLogFormatter("text", true), DeepEquals, &textFormatter{
DisableTimestamp: true,
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: 5 additions & 3 deletions server/core/region_kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import (
"sync"
"time"

"go.uber.org/zap"

"github.com/pingcap/kvproto/pkg/metapb"
log "github.com/pingcap/log"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

var dirtyFlushTick = time.Second
Expand Down Expand Up @@ -84,7 +86,7 @@ func (kv *RegionKV) backgroundFlush() {
continue
}
if err = kv.FlushRegion(); err != nil {
log.Error("flush regions error: ", err)
log.Error("flush regions meet error", zap.Error(err))
}
case <-kv.ctx.Done():
return
Expand Down Expand Up @@ -176,7 +178,7 @@ func (kv *RegionKV) flush() error {
func (kv *RegionKV) Close() error {
err := kv.FlushRegion()
if err != nil {
log.Error("meet error before close the region storage: ", err)
log.Error("meet error before close the region storage", zap.Error(err))
}
kv.cancel()
return kv.db.Close()
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
6 changes: 4 additions & 2 deletions server/core/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import (
"github.com/pingcap/errcode"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/pdpb"
log "github.com/sirupsen/logrus"
log "github.com/pingcap/log"
"go.uber.org/zap"
)

// StoreInfo contains information about a store.
Expand Down Expand Up @@ -529,7 +530,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))
}
s.stores[storeID] = store.Clone(SetStoreUnBlock())
}
Expand Down
2 changes: 2 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,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

0 comments on commit ce9508b

Please sign in to comment.