-
Notifications
You must be signed in to change notification settings - Fork 223
/
logs.go
59 lines (48 loc) · 1.19 KB
/
logs.go
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
55
56
57
58
59
package presenters
import (
"fmt"
"io"
"strings"
"github.com/logrusorgru/aurora"
"github.com/superfly/flyctl/api"
)
type LogPresenter struct {
RemoveNewlines bool
HideRegion bool
HideAllocID bool
}
func (lp *LogPresenter) FPrint(w io.Writer, entries []api.LogEntry) {
for _, entry := range entries {
lp.printEntry(w, entry)
}
}
var newLineReplacer = strings.NewReplacer("\r\n", aurora.Faint("↩︎").String(), "\n", aurora.Faint("↩︎").String())
var space = []byte(" ")
var newline = []byte("\n")
func (lp *LogPresenter) printEntry(w io.Writer, entry api.LogEntry) {
fmt.Fprintf(w, "%s ", aurora.Faint(entry.Timestamp))
if !lp.HideAllocID {
fmt.Fprintf(w, "%s ", entry.Instance)
}
if !lp.HideRegion {
fmt.Fprintf(w, "%s ", aurora.Green(entry.Region))
}
fmt.Fprintf(w, "[%s] ", aurora.Colorize(entry.Level, levelColor(entry.Level)))
if lp.RemoveNewlines {
newLineReplacer.WriteString(w, entry.Message)
} else {
w.Write([]byte(entry.Message))
}
w.Write(newline)
}
func levelColor(level string) aurora.Color {
switch level {
case "debug":
return aurora.CyanFg
case "info":
return aurora.BlueFg
case "warning":
return aurora.MagentaFg
}
return aurora.RedFg
}