forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_message.go
50 lines (41 loc) · 1.16 KB
/
log_message.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
package ui
import (
"fmt"
"strings"
"time"
"github.com/fatih/color"
)
// LogTimestampFormat is the timestamp formatting for log lines.
const LogTimestampFormat = "2006-01-02T15:04:05.00-0700"
//go:generate counterfeiter . LogMessage
// LogMessage is a log response representing one to many joined lines of a log
// message.
type LogMessage interface {
Message() string
Type() string
Timestamp() time.Time
SourceType() string
SourceInstance() string
}
// DisplayLogMessage formats and outputs a given log message.
func (ui *UI) DisplayLogMessage(message LogMessage, displayHeader bool) {
ui.terminalLock.Lock()
defer ui.terminalLock.Unlock()
var header string
if displayHeader {
time := message.Timestamp().In(ui.TimezoneLocation).Format(LogTimestampFormat)
header = fmt.Sprintf("%s [%s/%s] %s ",
time,
message.SourceType(),
message.SourceInstance(),
message.Type(),
)
}
for _, line := range strings.Split(message.Message(), "\n") {
logLine := fmt.Sprintf("%s%s", header, strings.TrimRight(line, "\r\n"))
if message.Type() == "ERR" {
logLine = ui.modifyColor(logLine, color.New(color.FgRed))
}
fmt.Fprintf(ui.Out, " %s\n", logLine)
}
}