github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/util/config.go (about)

     1  package util
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"strings"
     7  
     8  	"github.com/go-kit/log/level"
     9  	"github.com/prometheus/common/version"
    10  	"gopkg.in/yaml.v2"
    11  
    12  	util_log "github.com/grafana/loki/pkg/util/log"
    13  )
    14  
    15  // LogConfig takes a pointer to a config object, marshalls it to YAML and prints each line in REVERSE order
    16  // The reverse order makes display in Grafana in easier which typically sorts newest entries at the top.
    17  func LogConfig(cfg interface{}) error {
    18  	lc, err := yaml.Marshal(cfg)
    19  	if err != nil {
    20  		return err
    21  	}
    22  
    23  	cfgStr := string(lc)
    24  	cfgStrs := strings.Split(cfgStr, "\n")
    25  	for i := len(cfgStrs) - 1; i >= 0; i-- {
    26  		level.Info(util_log.Logger).Log("type", "config", "msg", cfgStrs[i])
    27  	}
    28  	return nil
    29  }
    30  
    31  // PrintConfig will takes a pointer to a config object, marshalls it to YAML and prints the result to the provided writer
    32  // unlike LogConfig, PrintConfig prints the object in naturally ocurring order.
    33  func PrintConfig(w io.Writer, config interface{}) error {
    34  	lc, err := yaml.Marshal(config)
    35  	if err != nil {
    36  		return err
    37  	}
    38  	fmt.Fprintf(w, "---\n# Loki Config\n# %s\n%s\n\n", version.Info(), string(lc))
    39  	return nil
    40  }