-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlog_unix.go
More file actions
54 lines (43 loc) · 1.32 KB
/
log_unix.go
File metadata and controls
54 lines (43 loc) · 1.32 KB
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
// +build linux darwin
package logging
import (
"fmt"
"io"
"strings"
"github.com/deis/pkg/prettyprint"
)
const colorStringEscape = "\033[3%dm"
// Choose an ANSI color by converting a string to an int.
//
// Color 5, magenta, reserved for controller log messages.
//
// Colors 0 and 7, black and white, are skipped because they are likely to be unreadable
// against terminal backgrounds. Instead, 9, the default text color is used, likely to be the color
// of the two that is readable against the terminal background.
func chooseColor(input string) string {
if input == "INFO" {
return fmt.Sprintf(colorStringEscape, 5)
}
var sum uint8
for _, char := range []byte(input) {
sum += uint8(char)
}
// Eight possible terminal colors, but Black and White are excluded
color := (sum % 6) + 1
// Color 5 is reserved, replace it with default text color.
if color == 5 {
color = 9
}
return fmt.Sprintf(colorStringEscape, color)
}
// PrintLog prints a log line with a color matched to its category.
func PrintLog(out io.Writer, log string) {
category := "unknown"
parts := strings.Split(strings.Split(log, " -- ")[0], " ")
category = parts[0]
colorVars := map[string]string{
"Color": chooseColor(category),
"Log": log,
}
fmt.Fprintln(out, prettyprint.ColorizeVars("{{.V.Color}}{{.V.Log}}{{.C.Default}}", colorVars))
}