Skip to content

Commit

Permalink
log: remove useless log (#4532)
Browse files Browse the repository at this point in the history
Signed-off-by: nolouch <nolouch@gmail.com>
  • Loading branch information
nolouch authored Jan 1, 2022
1 parent 27d7f63 commit 24bf11d
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 277 deletions.
6 changes: 0 additions & 6 deletions cmd/pd-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,6 @@ func main() {
// Flushing any buffered log entries
defer log.Sync()

// The old logger
err = logutil.InitLogger(&cfg.Log)
if err != nil {
log.Fatal("initialize logger error", errs.ZapError(err))
}

server.LogPDInfo()

for _, msg := range cfg.WarningMsgs {
Expand Down
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ require (
github.com/cakturk/go-netstat v0.0.0-20200220111822-e5b49efee7a5
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
github.com/coreos/go-semver v0.3.0
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f
github.com/docker/go-units v0.4.0
github.com/go-echarts/go-echarts v1.0.0
github.com/gogo/protobuf v1.3.1
Expand All @@ -38,7 +37,6 @@ require (
github.com/prometheus/client_golang v1.1.0
github.com/prometheus/common v0.6.0
github.com/sasha-s/go-deadlock v0.2.0
github.com/sirupsen/logrus v1.4.2
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5
github.com/swaggo/http-swagger v0.0.0-20200308142732-58ac5e232fba
Expand All @@ -53,6 +51,5 @@ require (
go.uber.org/zap v1.19.0
golang.org/x/tools v0.1.5
google.golang.org/grpc v1.26.0
gopkg.in/natefinch/lumberjack.v2 v2.0.0
gotest.tools/gotestsum v1.7.0
)
226 changes: 2 additions & 224 deletions pkg/logutil/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,13 @@
package logutil

import (
"bytes"
"fmt"
"os"
"path"
"runtime"
"strings"
"sync"
"sync/atomic"

"github.com/coreos/pkg/capnslog"
zaplog "github.com/pingcap/log"
log "github.com/sirupsen/logrus"
"github.com/tikv/pd/pkg/errs"
"go.etcd.io/etcd/raft"
"github.com/pingcap/log"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/grpc/grpclog"
"gopkg.in/natefinch/lumberjack.v2"
)

const (
defaultLogTimeFormat = "2006/01/02 15:04:05.000"
defaultLogMaxSize = 300 // MB
defaultLogFormat = "text"
defaultLogLevel = log.InfoLevel
)

// FileLogConfig serializes file log related config in toml/json.
Expand All @@ -66,86 +48,6 @@ type LogConfig struct {
File FileLogConfig `toml:"file" json:"file"`
}

// redirectFormatter will redirect etcd logs to logrus logs.
type redirectFormatter struct{}

// Format implements capnslog.Formatter hook.
func (rf *redirectFormatter) Format(pkg string, level capnslog.LogLevel, depth int, entries ...interface{}) {
if pkg != "" {
pkg = fmt.Sprint(pkg, ": ")
}

logStr := fmt.Sprint(pkg, entries)

switch level {
case capnslog.CRITICAL:
log.Fatalf(logStr)
case capnslog.ERROR:
log.Errorf(logStr)
case capnslog.WARNING:
log.Warningf(logStr)
case capnslog.NOTICE:
log.Infof(logStr)
case capnslog.INFO:
log.Infof(logStr)
case capnslog.DEBUG, capnslog.TRACE:
log.Debugf(logStr)
}
}

// Flush only for implementing Formatter.
func (rf *redirectFormatter) Flush() {}

// isSKippedPackageName tests whether path name is on log library calling stack.
func isSkippedPackageName(name string) bool {
return strings.Contains(name, "github.com/sirupsen/logrus") ||
strings.Contains(name, "github.com/coreos/pkg/capnslog")
}

// modifyHook injects file name and line pos into log entry.
type contextHook struct{}

// Fire implements logrus.Hook interface
// https://github.com/sirupsen/logrus/issues/63
func (hook *contextHook) Fire(entry *log.Entry) error {
pc := make([]uintptr, 4)
cnt := runtime.Callers(6, pc)

for i := 0; i < cnt; i++ {
fu := runtime.FuncForPC(pc[i] - 1)
name := fu.Name()
if !isSkippedPackageName(name) {
file, line := fu.FileLine(pc[i] - 1)
entry.Data["file"] = path.Base(file)
entry.Data["line"] = line
break
}
}
return nil
}

// Levels implements logrus.Hook interface.
func (hook *contextHook) Levels() []log.Level {
return log.AllLevels
}

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

// StringToZapLogLevel translates log level string to log level.
func StringToZapLogLevel(level string) zapcore.Level {
switch strings.ToLower(level) {
Expand All @@ -163,135 +65,11 @@ func StringToZapLogLevel(level string) zapcore.Level {
return zapcore.InfoLevel
}

// textFormatter is for compatibility with ngaut/log
type textFormatter struct {
DisableTimestamp bool
}

// Format implements logrus.Formatter
func (f *textFormatter) Format(entry *log.Entry) ([]byte, error) {
var b *bytes.Buffer
if entry.Buffer != nil {
b = entry.Buffer
} else {
b = &bytes.Buffer{}
}
if !f.DisableTimestamp {
fmt.Fprintf(b, "%s ", entry.Time.Format(defaultLogTimeFormat))
}
if file, ok := entry.Data["file"]; ok {
fmt.Fprintf(b, "%s:%v:", file, entry.Data["line"])
}
fmt.Fprintf(b, " [%s] %s", entry.Level.String(), entry.Message)
for k, v := range entry.Data {
if k != "file" && k != "line" {
fmt.Fprintf(b, " %v=%v", k, v)
}
}
b.WriteByte('\n')
return b.Bytes(), nil
}

// StringToLogFormatter uses the different log formatter according to a given format name.
func StringToLogFormatter(format string, disableTimestamp bool) log.Formatter {
switch strings.ToLower(format) {
case "text":
return &textFormatter{
DisableTimestamp: disableTimestamp,
}
case "json":
return &log.JSONFormatter{
TimestampFormat: defaultLogTimeFormat,
DisableTimestamp: disableTimestamp,
}
case "console":
return &log.TextFormatter{
FullTimestamp: true,
TimestampFormat: defaultLogTimeFormat,
DisableTimestamp: disableTimestamp,
}
default:
return &textFormatter{}
}
}

// InitFileLog initializes file based logging options.
func InitFileLog(cfg *zaplog.FileLogConfig) error {
if st, err := os.Stat(cfg.Filename); err == nil {
if st.IsDir() {
return errs.ErrInitFileLog.FastGenByArgs("can't use directory as log file name")
}
}
if cfg.MaxSize == 0 {
cfg.MaxSize = defaultLogMaxSize
}

// use lumberjack to logrotate
output := &lumberjack.Logger{
Filename: cfg.Filename,
MaxSize: cfg.MaxSize,
MaxBackups: cfg.MaxBackups,
MaxAge: cfg.MaxDays,
LocalTime: true,
}

log.SetOutput(output)
return nil
}

type wrapLogrus struct {
*log.Logger
}

// V provides the functionality that returns whether a particular log level is at
// least l - this is needed to meet the LoggerV2 interface. GRPC's logging levels
// are: https://github.com/grpc/grpc-go/blob/master/grpclog/loggerv2.go#L71
// 0=info, 1=warning, 2=error, 3=fatal
// logrus' are: https://github.com/sirupsen/logrus/blob/master/logrus.go
// 0=panic, 1=fatal, 2=error, 3=warn, 4=info, 5=debug
func (lg *wrapLogrus) V(l int) bool {
// translate to logrus level
logrusLevel := 4 - l
return int(lg.Logger.Level) <= logrusLevel
}

var once sync.Once

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

once.Do(func() {
log.SetLevel(StringToLogLevel(cfg.Level))
log.AddHook(&contextHook{})

if cfg.Format == "" {
cfg.Format = defaultLogFormat
}
log.SetFormatter(StringToLogFormatter(cfg.Format, cfg.DisableTimestamp))

// etcd log
capnslog.SetFormatter(&redirectFormatter{})
// grpc log
lg := &wrapLogrus{log.StandardLogger()}
grpclog.SetLoggerV2(lg)
// raft log
raft.SetLogger(lg)

if len(cfg.File.Filename) == 0 {
return
}

err = InitFileLog(&cfg.File)
})
return err
}

// LogPanic logs the panic reason and stack, then exit the process.
// Commonly used with a `defer`.
func LogPanic() {
if e := recover(); e != nil {
zaplog.Fatal("panic", zap.Reflect("recover", e))
log.Fatal("panic", zap.Reflect("recover", e))
}
}

Expand Down
42 changes: 1 addition & 41 deletions pkg/logutil/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@
package logutil

import (
"bytes"
"fmt"
"testing"

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

Expand All @@ -31,23 +28,7 @@ func Test(t *testing.T) {

var _ = Suite(&testLogSuite{})

type testLogSuite struct {
buf *bytes.Buffer
}

func (s *testLogSuite) SetUpSuite(c *C) {
s.buf = &bytes.Buffer{}
}

func (s *testLogSuite) TestStringToLogLevel(c *C) {
c.Assert(StringToLogLevel("fatal"), Equals, log.FatalLevel)
c.Assert(StringToLogLevel("ERROR"), Equals, log.ErrorLevel)
c.Assert(StringToLogLevel("warn"), Equals, log.WarnLevel)
c.Assert(StringToLogLevel("warning"), Equals, log.WarnLevel)
c.Assert(StringToLogLevel("debug"), Equals, log.DebugLevel)
c.Assert(StringToLogLevel("info"), Equals, log.InfoLevel)
c.Assert(StringToLogLevel("whatever"), Equals, log.InfoLevel)
}
type testLogSuite struct{}

func (s *testLogSuite) TestStringToZapLogLevel(c *C) {
c.Assert(StringToZapLogLevel("fatal"), Equals, zapcore.FatalLevel)
Expand All @@ -59,27 +40,6 @@ func (s *testLogSuite) TestStringToZapLogLevel(c *C) {
c.Assert(StringToZapLogLevel("whatever"), Equals, zapcore.InfoLevel)
}

func (s *testLogSuite) TestStringToLogFormatter(c *C) {
c.Assert(StringToLogFormatter("text", true), DeepEquals, &textFormatter{
DisableTimestamp: true,
})
c.Assert(StringToLogFormatter("json", true), DeepEquals, &log.JSONFormatter{
DisableTimestamp: true,
TimestampFormat: defaultLogTimeFormat,
})
c.Assert(StringToLogFormatter("console", true), DeepEquals, &log.TextFormatter{
DisableTimestamp: true,
FullTimestamp: true,
TimestampFormat: defaultLogTimeFormat,
})
c.Assert(StringToLogFormatter("", true), DeepEquals, &textFormatter{})
}

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)
}

func (s *testLogSuite) TestRedactLog(c *C) {
testcases := []struct {
name string
Expand Down
2 changes: 1 addition & 1 deletion server/api/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pingcap/kvproto/pkg/replication_modepb"
log "github.com/sirupsen/logrus"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/apiutil"
"github.com/tikv/pd/server"
"github.com/tikv/pd/server/core"
Expand Down
3 changes: 1 addition & 2 deletions tools/pd-simulator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (

"github.com/BurntSushi/toml"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/logutil"
"github.com/tikv/pd/server"
"github.com/tikv/pd/server/api"
"github.com/tikv/pd/server/config"
Expand Down Expand Up @@ -114,7 +113,7 @@ func NewSingleServer(ctx context.Context, simConfig *simulator.SimConfig) (*serv
log.Fatal("setup logger error", zap.Error(err))
}

err = logutil.InitLogger(&simConfig.ServerConfig.Log)
simConfig.ServerConfig.SetupLogger()
if err != nil {
log.Fatal("initialize logger error", zap.Error(err))
}
Expand Down

0 comments on commit 24bf11d

Please sign in to comment.