Skip to content

Commit

Permalink
Feature/add log dir (#908)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsvisa authored Nov 17, 2020
1 parent f9591eb commit ffa217f
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 11 deletions.
1 change: 1 addition & 0 deletions cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var envList = []string{
localdata.EnvNameSSHPassPrompt,
localdata.EnvNameSSHPath,
localdata.EnvNameSCPPath,
localdata.EnvNameLogPath,
}

func newEnvCmd() *cobra.Command {
Expand Down
2 changes: 1 addition & 1 deletion components/cluster/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion components/dm/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions pkg/localdata/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
21 changes: 12 additions & 9 deletions pkg/logger/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions tests/tiup-cluster/script/scale_tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions tests/tiup-dm/test_cmd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ffa217f

Please sign in to comment.