github.com/metasources/buildx@v0.0.0-20230418141019-7aa1459cedea/internal/config/logging.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/mitchellh/go-homedir"
     7  	"github.com/spf13/viper"
     8  
     9  	"github.com/anchore/go-logger"
    10  )
    11  
    12  // logging contains all logging-related configuration options available to the user via the application config.
    13  type logging struct {
    14  	Structured   bool         `yaml:"structured" json:"structured" mapstructure:"structured"` // show all log entries as JSON formatted strings
    15  	Level        logger.Level `yaml:"level" json:"level" mapstructure:"level"`                // the log level string hint
    16  	FileLocation string       `yaml:"file" json:"file-location" mapstructure:"file"`          // the file path to write logs to
    17  }
    18  
    19  func (cfg *logging) parseConfigValues() error {
    20  	if cfg.FileLocation != "" {
    21  		expandedPath, err := homedir.Expand(cfg.FileLocation)
    22  		if err != nil {
    23  			return fmt.Errorf("unable to expand log file path=%q: %w", cfg.FileLocation, err)
    24  		}
    25  		cfg.FileLocation = expandedPath
    26  	}
    27  	return nil
    28  }
    29  
    30  func (cfg logging) loadDefaultValues(v *viper.Viper) {
    31  	v.SetDefault("log.structured", false)
    32  	v.SetDefault("log.file", "")
    33  	v.SetDefault("log.level", string(logger.WarnLevel))
    34  }