diff --git a/client/cmd/apps.go b/client/cmd/apps.go index 968d1d8918..7820476c57 100644 --- a/client/cmd/apps.go +++ b/client/cmd/apps.go @@ -158,10 +158,19 @@ func AppLogs(appID string, lines int) error { return err } + return printLogs(logs) +} + +// printLogs prints each log line with a color matched to its category. +func printLogs(logs string) error { for _, log := range strings.Split(strings.Trim(logs, `\n`), `\n`) { - catagory := strings.Split(strings.Split(log, ": ")[0], " ")[1] + category := "unknown" + parts := strings.Split(strings.Split(log, ": ")[0], " ") + if len(parts) >= 2 { + category = parts[1] + } colorVars := map[string]string{ - "Color": chooseColor(catagory), + "Color": chooseColor(category), "Log": log, } fmt.Println(prettyprint.ColorizeVars("{{.V.Color}}{{.V.Log}}{{.C.Default}}", colorVars)) diff --git a/client/cmd/apps_test.go b/client/cmd/apps_test.go new file mode 100644 index 0000000000..5718789567 --- /dev/null +++ b/client/cmd/apps_test.go @@ -0,0 +1,18 @@ +package cmd + +import "testing" + +func TestPrintLogLinesBadLine(t *testing.T) { + t.Parallel() + + // Regression test for https://github.com/deis/deis/issues/4420 + logs := `\nDone preparing production files\n\n\u001b[4mRunning \"concat:plugins\" (concat) task\u001b[24m\n` + if err := printLogs(logs); err != nil { + t.Fatal(err) + } + + logs = `\n\n\n` + if err := printLogs(logs); err != nil { + t.Fatal(err) + } +}