diff --git a/cmd/env.go b/cmd/env.go index abfe241151..efc37b548f 100644 --- a/cmd/env.go +++ b/cmd/env.go @@ -28,6 +28,7 @@ var envList = []string{ localdata.EnvNameSSHPassPrompt, localdata.EnvNameSSHPath, localdata.EnvNameSCPPath, + localdata.EnvNameLogPath, } func newEnvCmd() *cobra.Command { diff --git a/components/cluster/command/root.go b/components/cluster/command/root.go index 41a1377466..cd516390d7 100644 --- a/components/cluster/command/root.go +++ b/components/cluster/command/root.go @@ -298,7 +298,7 @@ func Execute() { } if !errorx.HasTrait(err, errutil.ErrTraitPreCheck) { - logger.OutputDebugLog() + logger.OutputDebugLog("tiup-cluster") } if errx := errorx.Cast(err); errx != nil { diff --git a/components/dm/command/root.go b/components/dm/command/root.go index e994ff432a..34db5b1144 100644 --- a/components/dm/command/root.go +++ b/components/dm/command/root.go @@ -217,7 +217,7 @@ func Execute() { } if !errorx.HasTrait(err, errutil.ErrTraitPreCheck) { - logger.OutputDebugLog() + logger.OutputDebugLog("tiup-dm") } if errx := errorx.Cast(err); errx != nil { diff --git a/pkg/localdata/constant.go b/pkg/localdata/constant.go index f42d4cec15..c61ac3bbbf 100644 --- a/pkg/localdata/constant.go +++ b/pkg/localdata/constant.go @@ -81,6 +81,9 @@ const ( // EnvNameKeepSourceTarget is the variable name by which user can keep the source target or not EnvNameKeepSourceTarget = "TIUP_KEEP_SOURCE_TARGET" + // EnvNameLogPath is the variable name by which user can write the log files into + EnvNameLogPath = "TIUP_LOG_PATH" + // MetaFilename represents the process meta file name MetaFilename = "tiup_process_meta" ) diff --git a/pkg/logger/debug.go b/pkg/logger/debug.go index 6c358fc3b4..32ccdcf50d 100644 --- a/pkg/logger/debug.go +++ b/pkg/logger/debug.go @@ -22,6 +22,7 @@ import ( "time" "github.com/pingcap/tiup/pkg/colorutil" + "github.com/pingcap/tiup/pkg/localdata" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) @@ -35,20 +36,22 @@ func newDebugLogCore() zapcore.Core { } // OutputDebugLog outputs debug log in the current working directory. -func OutputDebugLog() { - if err := os.MkdirAll("./logs", 0755); err != nil { - _, _ = fmt.Fprintf(os.Stderr, "\nCreate debug logs directory failed %v.\n", err) +func OutputDebugLog(prefix string) { + logDir := os.Getenv(localdata.EnvNameLogPath) + if logDir == "" { + profile := localdata.InitProfile() + logDir = profile.Path("logs") + } + if err := os.MkdirAll(logDir, 0755); err != nil { + _, _ = fmt.Fprintf(os.Stderr, "\nCreate debug logs(%s) directory failed %v.\n", logDir, err) return } // FIXME: Stupid go does not allow writing fraction seconds without a leading dot. - fileName := time.Now().Format("./logs/tiup-cluster-debug-2006-01-02-15-04-05.log") - filePath, err := filepath.Abs(fileName) - if err != nil { - filePath = fileName - } + fileName := time.Now().Format(fmt.Sprintf("%s-debug-2006-01-02-15-04-05.log", prefix)) + filePath := filepath.Join(logDir, fileName) - err = ioutil.WriteFile(filePath, debugBuffer.Bytes(), 0644) + err := ioutil.WriteFile(filePath, debugBuffer.Bytes(), 0644) if err != nil { _, _ = colorutil.ColorWarningMsg.Fprint(os.Stderr, "\nWarn: Failed to write error debug log.\n") } else { diff --git a/tests/tiup-cluster/script/scale_tools.sh b/tests/tiup-cluster/script/scale_tools.sh index 80080382d5..844fe16e5a 100755 --- a/tests/tiup-cluster/script/scale_tools.sh +++ b/tests/tiup-cluster/script/scale_tools.sh @@ -100,4 +100,8 @@ function scale_tools() { fi tiup-cluster $client _test $name writable + + # test cluster log dir + tiup-cluster notfound-command 2>&1 | grep $HOME/.tiup/logs/tiup-cluster-debug + TIUP_LOG_PATH=/tmp/a/b tiup-cluster notfound-command 2>&1 | grep /tmp/a/b/tiup-cluster-debug } diff --git a/tests/tiup-dm/test_cmd.sh b/tests/tiup-dm/test_cmd.sh index 2398895dad..f2fb070ace 100755 --- a/tests/tiup-dm/test_cmd.sh +++ b/tests/tiup-dm/test_cmd.sh @@ -73,3 +73,7 @@ yes | tiup-dm scale-out $name $topo_worker ./script/task/run.sh tiup-dm --yes destroy $name + +# test dm log dir +tiup-dm notfound-command 2>&1 | grep $HOME/.tiup/logs/tiup-dm-debug +TIUP_LOG_PATH=/tmp/a/b tiup-dm notfound-command 2>&1 | grep /tmp/a/b/tiup-dm-debug