Skip to content

Commit dfe19e0

Browse files
feat(logging): extract logging to its own package and write tests (#166)
1 parent 6f11e8d commit dfe19e0

9 files changed

Lines changed: 87 additions & 43 deletions

File tree

cmd/apps.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ import (
66
"strings"
77
"time"
88

9-
"github.com/deis/pkg/prettyprint"
10-
119
"github.com/deis/controller-sdk-go/api"
1210
"github.com/deis/controller-sdk-go/apps"
1311
"github.com/deis/controller-sdk-go/config"
1412
"github.com/deis/controller-sdk-go/domains"
1513
"github.com/deis/workflow-cli/pkg/git"
14+
"github.com/deis/workflow-cli/pkg/logging"
1615
"github.com/deis/workflow-cli/pkg/webbrowser"
1716
"github.com/deis/workflow-cli/settings"
1817
)
@@ -177,20 +176,8 @@ func AppLogs(appID string, lines int) error {
177176
return err
178177
}
179178

180-
return printLogs(logs)
181-
}
182-
183-
// printLogs prints each log line with a color matched to its category.
184-
func printLogs(logs string) error {
185-
for _, log := range strings.Split(logs, `\n`) {
186-
category := "unknown"
187-
parts := strings.Split(strings.Split(log, " -- ")[0], " ")
188-
category = parts[0]
189-
colorVars := map[string]string{
190-
"Color": chooseColor(category),
191-
"Log": log,
192-
}
193-
fmt.Println(prettyprint.ColorizeVars("{{.V.Color}}{{.V.Log}}{{.C.Default}}", colorVars))
179+
for _, log := range strings.Split(strings.TrimRight(logs, `\n`), `\n`) {
180+
logging.PrintLog(os.Stdout, log)
194181
}
195182

196183
return nil

cmd/apps_test.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,6 @@ package cmd
22

33
import "testing"
44

5-
func TestPrintLogLinesBadLine(t *testing.T) {
6-
t.Parallel()
7-
8-
// Regression test for https://github.com/deis/deis/issues/4420
9-
logs := `\nDone preparing production files\n\n\u001b[4mRunning \"concat:plugins\" (concat) task\u001b[24m\n`
10-
if err := printLogs(logs); err != nil {
11-
t.Fatal(err)
12-
}
13-
14-
logs = `\n\n\n`
15-
if err := printLogs(logs); err != nil {
16-
t.Fatal(err)
17-
}
18-
}
19-
205
type expandURLCases struct {
216
Input string
227
Expected string

cmd/colors_windows.go

Lines changed: 0 additions & 8 deletions
This file was deleted.

glide.lock

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

glide.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ import:
1414
- ssh/terminal
1515
- package: gopkg.in/yaml.v2
1616
- package: github.com/olekukonko/tablewriter
17+
- package: github.com/arschles/assert
1718
- package: github.com/deis/controller-sdk-go
1819
version: 09f8465bf5598f84987734082d22f4201632a320

pkg/logging/doc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Package logging is used to print deis application logs with colored output, when supported.
2+
package logging
Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
// +build linux darwin
22

3-
package cmd
3+
package logging
44

5-
import "fmt"
5+
import (
6+
"fmt"
7+
"io"
8+
"strings"
9+
10+
"github.com/deis/pkg/prettyprint"
11+
)
612

713
const colorStringEscape = "\033[3%dm"
814

@@ -34,3 +40,15 @@ func chooseColor(input string) string {
3440

3541
return fmt.Sprintf(colorStringEscape, color)
3642
}
43+
44+
// PrintLog prints a log line with a color matched to its category.
45+
func PrintLog(out io.Writer, log string) {
46+
category := "unknown"
47+
parts := strings.Split(strings.Split(log, " -- ")[0], " ")
48+
category = parts[0]
49+
colorVars := map[string]string{
50+
"Color": chooseColor(category),
51+
"Log": log,
52+
}
53+
fmt.Fprintln(out, prettyprint.ColorizeVars("{{.V.Color}}{{.V.Log}}{{.C.Default}}", colorVars))
54+
}

pkg/logging/log_unix_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// +build linux darwin
2+
3+
package logging
4+
5+
import (
6+
"bytes"
7+
"testing"
8+
9+
"github.com/arschles/assert"
10+
)
11+
12+
type colorsTestCase struct {
13+
Input string
14+
Expected string
15+
}
16+
17+
func TestChooseColor(t *testing.T) {
18+
t.Parallel()
19+
20+
colors := []colorsTestCase{
21+
{"INFO", "\033[35m"},
22+
{"a", "\033[32m"},
23+
{"b", "\033[33m"},
24+
{"c", "\033[34m"},
25+
{"d", "\033[39m"},
26+
{"e", "\033[36m"},
27+
{"f", "\033[31m"},
28+
}
29+
for _, color := range colors {
30+
assert.Equal(t, chooseColor(color.Input), color.Expected, "color")
31+
}
32+
}
33+
34+
func TestPrintLogs(t *testing.T) {
35+
var b bytes.Buffer
36+
PrintLog(&b, "INFO [test]: testing")
37+
assert.Equal(t, b.String(), "\033[35mINFO [test]: testing\033[0m\n", "log line")
38+
b.Reset()
39+
// Regression test for https://github.com/deis/deis/issues/4420
40+
PrintLog(&b, "\nDone preparing production files\n\n\u001b[4mRunning \"concat:plugins\" (concat) task\u001b[24m\n")
41+
assert.Equal(t, b.String(),
42+
"\033[31m\nDone preparing production files\n\n\u001b[4mRunning \"concat:plugins\" (concat) task\u001b[24m\n\033[0m\n", "log line")
43+
}

pkg/logging/log_windows.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// +build windows
2+
3+
package logging
4+
5+
import (
6+
"fmt"
7+
"io"
8+
)
9+
10+
// PrintLog prints each log line. Windows doesn't support ansi escape codes,
11+
// so color is not printed.
12+
func PrintLog(out io.Writer, log string) {
13+
fmt.Fprintln(out, log)
14+
}

0 commit comments

Comments
 (0)