github.com/cloudfoundry-attic/ltc@v0.0.0-20151123212628-098adc7919fc/logs/console_tailed_logs_outputter/console_tailed_logs_outputter.go (about)

     1  package console_tailed_logs_outputter
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/cloudfoundry-incubator/ltc/logs"
     8  	"github.com/cloudfoundry-incubator/ltc/logs/console_tailed_logs_outputter/prettify"
     9  	"github.com/cloudfoundry-incubator/ltc/logs/reserved_app_ids"
    10  	"github.com/cloudfoundry-incubator/ltc/terminal"
    11  	"github.com/cloudfoundry-incubator/ltc/terminal/colors"
    12  	"github.com/cloudfoundry/sonde-go/events"
    13  )
    14  
    15  type TailedLogsOutputter interface {
    16  	OutputDebugLogs(pretty bool)
    17  	OutputTailedLogs(appGuid string)
    18  	StopOutputting()
    19  }
    20  
    21  type ConsoleTailedLogsOutputter struct {
    22  	outputChan chan string
    23  	ui         terminal.UI
    24  	logReader  logs.LogReader
    25  }
    26  
    27  func NewConsoleTailedLogsOutputter(ui terminal.UI, logReader logs.LogReader) *ConsoleTailedLogsOutputter {
    28  	return &ConsoleTailedLogsOutputter{
    29  		outputChan: make(chan string, 10),
    30  		ui:         ui,
    31  		logReader:  logReader,
    32  	}
    33  
    34  }
    35  
    36  func (ctlo *ConsoleTailedLogsOutputter) OutputDebugLogs(pretty bool) {
    37  	if pretty {
    38  		go ctlo.logReader.TailLogs(reserved_app_ids.LatticeDebugLogStreamAppId, ctlo.prettyDebugLogCallback, ctlo.prettyDebugErrorCallback)
    39  	} else {
    40  		go ctlo.logReader.TailLogs(reserved_app_ids.LatticeDebugLogStreamAppId, ctlo.rawDebugLogCallback, ctlo.rawDebugErrorCallback)
    41  	}
    42  	for log := range ctlo.outputChan {
    43  		ctlo.ui.SayLine(log)
    44  	}
    45  }
    46  
    47  func (ctlo *ConsoleTailedLogsOutputter) OutputTailedLogs(appGuid string) {
    48  	go ctlo.logReader.TailLogs(appGuid, ctlo.logCallback, ctlo.errorCallback)
    49  
    50  	for log := range ctlo.outputChan {
    51  		ctlo.ui.SayLine(log)
    52  	}
    53  }
    54  
    55  func (ctlo *ConsoleTailedLogsOutputter) StopOutputting() {
    56  	ctlo.logReader.StopTailing()
    57  }
    58  
    59  func (ctlo *ConsoleTailedLogsOutputter) logCallback(log *events.LogMessage) {
    60  	timeString := time.Unix(0, log.GetTimestamp()).Format("01/02 15:04:05.00")
    61  	logOutput := fmt.Sprintf("%s [%s|%s] %s", colors.Cyan(timeString), colors.Yellow(log.GetSourceType()), colors.Yellow(log.GetSourceInstance()), log.GetMessage())
    62  	ctlo.outputChan <- logOutput
    63  }
    64  
    65  func (ctlo *ConsoleTailedLogsOutputter) errorCallback(err error) {
    66  	ctlo.outputChan <- err.Error()
    67  }
    68  
    69  func (ctlo *ConsoleTailedLogsOutputter) prettyDebugLogCallback(log *events.LogMessage) {
    70  	ctlo.outputChan <- prettify.Prettify(log)
    71  }
    72  
    73  func (ctlo *ConsoleTailedLogsOutputter) prettyDebugErrorCallback(err error) {
    74  	ctlo.outputChan <- err.Error()
    75  }
    76  
    77  func (ctlo *ConsoleTailedLogsOutputter) rawDebugLogCallback(log *events.LogMessage) {
    78  	timeString := time.Unix(0, log.GetTimestamp()).Format("01/02 15:04:05.00")
    79  	logOutput := fmt.Sprintf("%s [%s|%s] %s", timeString, log.GetSourceType(), log.GetSourceInstance(), log.GetMessage())
    80  	ctlo.outputChan <- logOutput
    81  }
    82  
    83  func (ctlo *ConsoleTailedLogsOutputter) rawDebugErrorCallback(err error) {
    84  	ctlo.outputChan <- err.Error()
    85  }