/
cmd.go
146 lines (131 loc) · 4.31 KB
/
cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0.
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"sync"
"sync/atomic"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/log"
"github.com/spf13/cobra"
"github.com/tikv/migration/br/pkg/redact"
"github.com/tikv/migration/br/pkg/summary"
"github.com/tikv/migration/br/pkg/task"
"github.com/tikv/migration/br/pkg/version/build"
)
var (
initOnce = sync.Once{}
defaultContext context.Context
hasLogFile uint64
envLogToTermKey = "BR_LOG_TO_TERM"
)
const (
// FlagLogLevel is the name of log-level flag.
FlagLogLevel = "log-level"
// FlagLogFile is the name of log-file flag.
FlagLogFile = "log-file"
// FlagLogFormat is the name of log-format flag.
FlagLogFormat = "log-format"
// FlagStatusAddr is the name of status-addr flag.
FlagStatusAddr = "status-addr"
// FlagSlowLogFile is the name of slow-log-file flag.
FlagSlowLogFile = "slow-log-file"
// FlagRedactLog is whether to redact sensitive information in log, already deprecated by FlagRedactInfoLog
FlagRedactLog = "redact-log"
// FlagRedactInfoLog is whether to redact sensitive information in log.
FlagRedactInfoLog = "redact-info-log"
flagVersion = "version"
flagVersionShort = "V"
)
func timestampLogFileName() string {
return filepath.Join(os.TempDir(), time.Now().Format("br.log.2006-01-02T15.04.05Z0700"))
}
// AddFlags adds flags to the given cmd.
func AddFlags(cmd *cobra.Command) {
cmd.Version = build.Info()
cmd.Flags().BoolP(flagVersion, flagVersionShort, false, "Display version information about BR")
cmd.SetVersionTemplate("{{printf \"%s\" .Version}}\n")
cmd.PersistentFlags().StringP(FlagLogLevel, "L", "info",
"Set the log level")
cmd.PersistentFlags().String(FlagLogFile, timestampLogFileName(),
"Set the log file path. If not set, logs will output to temp file")
cmd.PersistentFlags().String(FlagLogFormat, "text",
"Set the log format")
cmd.PersistentFlags().Bool(FlagRedactLog, false,
"Set whether to redact sensitive info in log, already deprecated by --redact-info-log")
cmd.PersistentFlags().Bool(FlagRedactInfoLog, false,
"Set whether to redact sensitive info in log")
cmd.PersistentFlags().String(FlagStatusAddr, "",
"Set the HTTP listening address for the status report service. Set to empty string to disable")
task.DefineCommonFlags(cmd.PersistentFlags())
cmd.PersistentFlags().StringP(FlagSlowLogFile, "", "",
"Set the slow log file path. If not set, discard slow logs")
_ = cmd.PersistentFlags().MarkHidden(FlagSlowLogFile)
_ = cmd.PersistentFlags().MarkHidden(FlagRedactLog)
_ = cmd.PersistentFlags().MarkHidden(FlagRedactInfoLog)
_ = cmd.PersistentFlags().MarkHidden(FlagLogFormat)
_ = cmd.PersistentFlags().MarkHidden(FlagStatusAddr)
}
// Init initializes BR cli.
func Init(cmd *cobra.Command) (err error) {
initOnce.Do(func() {
// Initialize the logger.
conf := new(log.Config)
conf.Level, err = cmd.Flags().GetString(FlagLogLevel)
if err != nil {
return
}
conf.File.Filename, err = cmd.Flags().GetString(FlagLogFile)
if err != nil {
return
}
conf.Format, err = cmd.Flags().GetString(FlagLogFormat)
if err != nil {
return
}
_, outputLogToTerm := os.LookupEnv(envLogToTermKey)
if outputLogToTerm {
// Log to term if env `BR_LOG_TO_TERM` is set.
conf.File.Filename = ""
}
if len(conf.File.Filename) != 0 {
atomic.StoreUint64(&hasLogFile, 1)
summary.InitCollector(true)
// cmd.PrintErr prints to stderr, but PrintErrf prints to stdout.
cmd.PrintErr(fmt.Sprintf("Detail BR log in %s \n", conf.File.Filename))
}
lg, p, e := log.InitLogger(conf)
if e != nil {
err = e
return
}
log.ReplaceGlobals(lg, p)
redactLog, e := cmd.Flags().GetBool(FlagRedactLog)
if e != nil {
err = e
return
}
redactInfoLog, e := cmd.Flags().GetBool(FlagRedactInfoLog)
if e != nil {
err = e
return
}
redact.InitRedact(redactLog || redactInfoLog)
})
return errors.Trace(err)
}
// HasLogFile returns whether we set a log file.
func HasLogFile() bool {
return atomic.LoadUint64(&hasLogFile) != uint64(0)
}
// SetDefaultContext sets the default context for command line usage.
func SetDefaultContext(ctx context.Context) {
defaultContext = ctx
}
// GetDefaultContext returns the default context for command line usage.
func GetDefaultContext() context.Context {
return defaultContext
}