github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/logcli/output/output.go (about) 1 package output 2 3 import ( 4 "fmt" 5 "hash/fnv" 6 "io" 7 "time" 8 9 "github.com/fatih/color" 10 11 "github.com/grafana/loki/pkg/loghttp" 12 ) 13 14 // Blue color is excluded since we are already printing timestamp 15 // in blue color 16 var colorList = []*color.Color{ 17 color.New(color.FgHiCyan), 18 color.New(color.FgCyan), 19 color.New(color.FgHiGreen), 20 color.New(color.FgGreen), 21 color.New(color.FgHiMagenta), 22 color.New(color.FgMagenta), 23 color.New(color.FgHiYellow), 24 color.New(color.FgYellow), 25 color.New(color.FgHiRed), 26 color.New(color.FgRed), 27 } 28 29 // LogOutput is the interface any output mode must implement 30 type LogOutput interface { 31 FormatAndPrintln(ts time.Time, lbls loghttp.LabelSet, maxLabelsLen int, line string) 32 } 33 34 // LogOutputOptions defines options supported by LogOutput 35 type LogOutputOptions struct { 36 Timezone *time.Location 37 NoLabels bool 38 ColoredOutput bool 39 } 40 41 // NewLogOutput creates a log output based on the input mode and options 42 func NewLogOutput(w io.Writer, mode string, options *LogOutputOptions) (LogOutput, error) { 43 if options.Timezone == nil { 44 options.Timezone = time.Local 45 } 46 47 switch mode { 48 case "default": 49 return &DefaultOutput{ 50 w: w, 51 options: options, 52 }, nil 53 case "jsonl": 54 return &JSONLOutput{ 55 w: w, 56 options: options, 57 }, nil 58 case "raw": 59 return &RawOutput{ 60 w: w, 61 options: options, 62 }, nil 63 default: 64 return nil, fmt.Errorf("unknown log output mode '%s'", mode) 65 } 66 } 67 68 func getColor(labels string) *color.Color { 69 hash := fnv.New32() 70 _, _ = hash.Write([]byte(labels)) 71 id := hash.Sum32() % uint32(len(colorList)) 72 color := colorList[id] 73 return color 74 }