From ce9508b9d33745fb41250aea37f70aa05e4fa81c Mon Sep 17 00:00:00 2001 From: nolouch Date: Fri, 22 Feb 2019 16:24:23 +0800 Subject: [PATCH] *: log format for logutil,server/core Signed-off-by: nolouch --- pkg/etcdutil/etcdutil.go | 2 +- pkg/logutil/log.go | 22 ++++++++++++++++++++-- pkg/logutil/log_test.go | 11 +++++++++++ server/api/log.go | 4 ++-- server/cache/ttl.go | 5 +++-- server/core/region_kv.go | 8 +++++--- server/core/region_tree.go | 8 ++++++-- server/core/store.go | 6 ++++-- server/server.go | 2 ++ 9 files changed, 54 insertions(+), 14 deletions(-) diff --git a/pkg/etcdutil/etcdutil.go b/pkg/etcdutil/etcdutil.go index 762898acc5a..84ffc9fd3a1 100644 --- a/pkg/etcdutil/etcdutil.go +++ b/pkg/etcdutil/etcdutil.go @@ -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 ( diff --git a/pkg/logutil/log.go b/pkg/logutil/log.go index 1f7c2b33f0d..7ffb02017a4 100644 --- a/pkg/logutil/log.go +++ b/pkg/logutil/log.go @@ -19,7 +19,6 @@ import ( "os" "path" "runtime" - "runtime/debug" "strings" "sync" @@ -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" ) @@ -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 @@ -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)) } } diff --git a/pkg/logutil/log_test.go b/pkg/logutil/log_test.go index 258a884227a..7d397f5356a 100644 --- a/pkg/logutil/log_test.go +++ b/pkg/logutil/log_test.go @@ -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 ( @@ -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, diff --git a/server/api/log.go b/server/api/log.go index 714909f9f18..461e330cdd6 100644 --- a/server/api/log.go +++ b/server/api/log.go @@ -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" ) @@ -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) } diff --git a/server/cache/ttl.go b/server/cache/ttl.go index e0dd4d325e1..afb2823f3a4 100644 --- a/server/cache/ttl.go +++ b/server/cache/ttl.go @@ -17,7 +17,8 @@ import ( "sync" "time" - log "github.com/sirupsen/logrus" + log "github.com/pingcap/log" + "go.uber.org/zap" ) type ttlCacheItem struct { @@ -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)) } } diff --git a/server/core/region_kv.go b/server/core/region_kv.go index be1ad86a80a..cdf82d2041e 100644 --- a/server/core/region_kv.go +++ b/server/core/region_kv.go @@ -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 @@ -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 @@ -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() diff --git a/server/core/region_tree.go b/server/core/region_tree.go index ba6ac83c212..2fc96a1cc6e 100644 --- a/server/core/region_tree.go +++ b/server/core/region_tree.go @@ -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 = ®ionItem{} @@ -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(®ionItem{item}) } diff --git a/server/core/store.go b/server/core/store.go index 75819bf438a..8e7156d8624 100644 --- a/server/core/store.go +++ b/server/core/store.go @@ -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. @@ -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()) } diff --git a/server/server.go b/server/server.go index 810d9fac86e..790b63c2b28 100644 --- a/server/server.go +++ b/server/server.go @@ -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"