github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/util/ui/log_message.go (about) 1 package ui 2 3 import ( 4 "fmt" 5 "strings" 6 "time" 7 8 "github.com/fatih/color" 9 ) 10 11 // LogTimestampFormat is the timestamp formatting for log lines. 12 const LogTimestampFormat = "2006-01-02T15:04:05.00-0700" 13 14 //go:generate counterfeiter . LogMessage 15 16 // LogMessage is a log response representing one to many joined lines of a log 17 // message. 18 type LogMessage interface { 19 Message() string 20 Type() string 21 Timestamp() time.Time 22 SourceType() string 23 SourceInstance() string 24 } 25 26 // DisplayLogMessage formats and outputs a given log message. 27 func (ui *UI) DisplayLogMessage(message LogMessage, displayHeader bool) { 28 ui.terminalLock.Lock() 29 defer ui.terminalLock.Unlock() 30 31 var header string 32 if displayHeader { 33 time := message.Timestamp().In(ui.TimezoneLocation).Format(LogTimestampFormat) 34 35 header = fmt.Sprintf("%s [%s/%s] %s ", 36 time, 37 message.SourceType(), 38 message.SourceInstance(), 39 message.Type(), 40 ) 41 } 42 43 for _, line := range strings.Split(message.Message(), "\n") { 44 logLine := fmt.Sprintf("%s%s", header, strings.TrimRight(line, "\r\n")) 45 if message.Type() == "ERR" { 46 logLine = ui.modifyColor(logLine, color.New(color.FgRed)) 47 } 48 fmt.Fprintf(ui.Out, " %s\n", logLine) 49 } 50 }