Skip to content

Commit

Permalink
cmd: discard slog log by default (pingcap#52)
Browse files Browse the repository at this point in the history
Signed-off-by: Neil Shen <overvenus@gmail.com>
  • Loading branch information
overvenus authored Nov 13, 2019
1 parent 1daf2b7 commit ce2c09b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
25 changes: 25 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

"github.com/pingcap/errors"
"github.com/pingcap/log"
"github.com/pingcap/tidb/util/logutil"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"go.uber.org/zap"

Expand Down Expand Up @@ -40,6 +42,8 @@ const (
FlagLogFile = "log-file"
// FlagStatusAddr is the name of status-addr flag.
FlagStatusAddr = "status-addr"
// FlagSlowLogFile is the name of slow-log-file flag.
FlagSlowLogFile = "slow-log-file"
)

// AddFlags adds flags to the given cmd.
Expand All @@ -56,6 +60,10 @@ func AddFlags(cmd *cobra.Command) {
"Set the log file path. If not set, logs will output to stdout")
cmd.PersistentFlags().String(FlagStatusAddr, "",
"Set the HTTP listening address for the status report service. Set to empty string to disable")

cmd.PersistentFlags().StringP(FlagSlowLogFile, "", "",
"Set the slow log file path. If not set, discard slow logs")
_ = cmd.PersistentFlags().MarkHidden(FlagSlowLogFile)
}

// Init ...
Expand All @@ -78,6 +86,23 @@ func Init(cmd *cobra.Command) (err error) {
}
log.ReplaceGlobals(lg, p)

slowLogFilename, e := cmd.Flags().GetString(FlagSlowLogFile)
if e != nil {
err = e
return
}
if len(slowLogFilename) != 0 {
slowCfg := logutil.LogConfig{SlowQueryFile: slowLogFilename}
e = logutil.InitLogger(&slowCfg)
if e != nil {
err = e
return
}
} else {
// Hack! Discard slow log by setting log level to PanicLevel
logutil.SlowQueryLogger.SetLevel(logrus.PanicLevel)
}

// Initialize the pprof server.
statusAddr, e := cmd.Flags().GetString(FlagStatusAddr)
if e != nil {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
github.com/prometheus/client_golang v1.0.0
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 // indirect
github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd // indirect
github.com/sirupsen/logrus v1.2.0
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.3
go.uber.org/zap v1.10.0
Expand Down

0 comments on commit ce2c09b

Please sign in to comment.