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

     1  package prettify
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"path"
     7  	"strconv"
     8  	"strings"
     9  	"time"
    10  
    11  	"github.com/cloudfoundry-incubator/ltc/logs/console_tailed_logs_outputter/chug"
    12  	"github.com/cloudfoundry-incubator/ltc/terminal/colors"
    13  	"github.com/cloudfoundry/sonde-go/events"
    14  	"github.com/pivotal-golang/lager"
    15  )
    16  
    17  var colorLookup = map[string]string{
    18  	"rep":          colors.ColorBlue,
    19  	"garden-linux": colors.ColorPurple,
    20  }
    21  
    22  func Prettify(logMessage *events.LogMessage) string {
    23  	entry := chug.ChugLogMessage(logMessage)
    24  
    25  	var sourceType, sourceTypeColorized, sourceInstance, sourceInstanceColorized string
    26  
    27  	sourceType = path.Base(entry.LogMessage.GetSourceType())
    28  	sourceInstance = entry.LogMessage.GetSourceInstance()
    29  
    30  	// TODO: Or, do we use GetSourceType() for raw and Json source for pretty?
    31  	color, ok := colorLookup[path.Base(strings.Split(sourceType, ":")[0])]
    32  	if ok {
    33  		sourceTypeColorized = colors.Colorize(color, sourceType)
    34  		sourceInstanceColorized = colors.Colorize(color, sourceInstance)
    35  	} else {
    36  		sourceTypeColorized = sourceType
    37  		sourceInstanceColorized = sourceInstance
    38  	}
    39  
    40  	prefix := fmt.Sprintf("[%s|%s]", sourceTypeColorized, sourceInstanceColorized)
    41  
    42  	colorWidth := len(sourceTypeColorized+sourceInstanceColorized) - len(sourceType+sourceInstance)
    43  
    44  	components := append([]string(nil), fmt.Sprintf("%-"+strconv.Itoa(34+colorWidth)+"s", prefix))
    45  
    46  	var whichFunc func(chug.Entry) []string
    47  	if entry.IsLager {
    48  		whichFunc = prettyPrintLog
    49  	} else {
    50  		whichFunc = prettyPrintRaw
    51  	}
    52  
    53  	components = append(components, whichFunc(entry)...)
    54  	return strings.Join(components, " ")
    55  }
    56  
    57  func prettyPrintLog(entry chug.Entry) []string {
    58  	var logColor, level string
    59  	switch entry.Log.LogLevel {
    60  	case lager.INFO:
    61  		logColor = colors.ColorDefault
    62  		level = "[INFO]"
    63  	case lager.DEBUG:
    64  		logColor = colors.ColorGray
    65  		level = "[DEBUG]"
    66  	case lager.ERROR:
    67  		logColor = colors.ColorRed
    68  		level = "[ERROR]"
    69  	case lager.FATAL:
    70  		logColor = colors.ColorRed
    71  		level = "[FATAL]"
    72  	}
    73  
    74  	var components []string
    75  	components = append(components, colors.Colorize(logColor, "%-9s", level))
    76  
    77  	timestamp := entry.Log.Timestamp.Format("01/02 15:04:05.00")
    78  	components = append(components, colors.Colorize(logColor, "%-17s", timestamp))
    79  	components = append(components, colors.Colorize(logColor, "%-14s", entry.Log.Session))
    80  	components = append(components, colors.Colorize(logColor, entry.Log.Message))
    81  
    82  	if entry.Log.Error != nil {
    83  		components = append(components, fmt.Sprintf("\n%s%s", strings.Repeat(" ", 66), colors.Colorize(logColor, entry.Log.Error.Error())))
    84  	}
    85  
    86  	if len(entry.Log.Data) > 0 {
    87  		dataJSON, _ := json.Marshal(entry.Log.Data)
    88  		components = append(components, fmt.Sprintf("\n%s%s", strings.Repeat(" ", 66), colors.Colorize(logColor, string(dataJSON))))
    89  	}
    90  
    91  	return components
    92  }
    93  
    94  func prettyPrintRaw(entry chug.Entry) []string {
    95  	var components []string
    96  	components = append(components, strings.Repeat(" ", 9)) // loglevel
    97  	timestamp := time.Unix(0, entry.LogMessage.GetTimestamp())
    98  	components = append(components, fmt.Sprintf("%-17s", timestamp.Format("01/02 15:04:05.00")))
    99  	components = append(components, strings.Repeat(" ", 14)) // sesh
   100  	components = append(components, string(entry.Raw))
   101  
   102  	return components
   103  }