github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/logcli/output/default.go (about) 1 package output 2 3 import ( 4 "fmt" 5 "io" 6 "strings" 7 "time" 8 9 "github.com/fatih/color" 10 11 "github.com/grafana/loki/pkg/loghttp" 12 ) 13 14 // DefaultOutput provides logs and metadata in human readable format 15 type DefaultOutput struct { 16 w io.Writer 17 options *LogOutputOptions 18 } 19 20 // Format a log entry in a human readable format 21 func (o *DefaultOutput) FormatAndPrintln(ts time.Time, lbls loghttp.LabelSet, maxLabelsLen int, line string) { 22 timestamp := ts.In(o.options.Timezone).Format(time.RFC3339) 23 line = strings.TrimSpace(line) 24 25 if o.options.NoLabels { 26 fmt.Fprintf(o.w, "%s %s\n", color.BlueString(timestamp), line) 27 return 28 } 29 if o.options.ColoredOutput { 30 labelsColor := getColor(lbls.String()).SprintFunc() 31 fmt.Fprintf(o.w, "%s %s %s\n", color.BlueString(timestamp), labelsColor(padLabel(lbls, maxLabelsLen)), line) 32 } else { 33 fmt.Fprintf(o.w, "%s %s %s\n", color.BlueString(timestamp), color.RedString(padLabel(lbls, maxLabelsLen)), line) 34 } 35 36 } 37 38 // add some padding after labels 39 func padLabel(ls loghttp.LabelSet, maxLabelsLen int) string { 40 labels := ls.String() 41 if len(labels) < maxLabelsLen { 42 labels += strings.Repeat(" ", maxLabelsLen-len(labels)) 43 } 44 return labels 45 }