diff --git a/cmd/root.go b/cmd/root.go index 0856b741..4346ffde 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -92,7 +92,7 @@ func LoadConfig(c *config.LuetConfig) error { NewSpinner() - if c.GetLogging().Path != "" { + if c.GetLogging().EnableLogFile && c.GetLogging().Path != "" { // Init zap logger err = ZapLogger() if err != nil { @@ -152,6 +152,9 @@ func init() { pflags.StringVar(&cfgFile, "config", "", "config file (default is $HOME/.luet.yaml)") pflags.BoolP("debug", "d", false, "verbose output") pflags.Bool("fatal", false, "Enables Warnings to exit") + pflags.Bool("enable-logfile", false, "Enable log to file") + pflags.StringP("logfile", "l", config.LuetCfg.GetLogging().Path, + "Logfile path. Empty value disable log to file.") sameOwner := false u, err := user.Current() @@ -166,10 +169,13 @@ func init() { pflags.Bool("same-owner", sameOwner, "Maintain same owner on uncompress.") pflags.Int("concurrency", runtime.NumCPU(), "Concurrency") - config.LuetCfg.Viper.BindPFlag("general.same_owner", pflags.Lookup("same-owner")) - config.LuetCfg.Viper.BindPFlag("general.debug", pflags.Lookup("debug")) + config.LuetCfg.Viper.BindPFlag("logging.enable_logfile", pflags.Lookup("enable-logfile")) + config.LuetCfg.Viper.BindPFlag("logging.path", pflags.Lookup("logfile")) + config.LuetCfg.Viper.BindPFlag("general.concurrency", pflags.Lookup("concurrency")) + config.LuetCfg.Viper.BindPFlag("general.debug", pflags.Lookup("debug")) config.LuetCfg.Viper.BindPFlag("general.fatal_warnings", pflags.Lookup("fatal")) + config.LuetCfg.Viper.BindPFlag("general.same_owner", pflags.Lookup("same-owner")) } // initConfig reads in config file and ENV variables if set. diff --git a/contrib/config/luet.yaml b/contrib/config/luet.yaml index c1f31089..9835442d 100644 --- a/contrib/config/luet.yaml +++ b/contrib/config/luet.yaml @@ -4,8 +4,11 @@ # Logging configuration section: # --------------------------------------------- # logging: +# # Enable loggging to file (if path is not empty) +# enable_logfile: false +# # Leave empty to skip logging to file. -# path: "" +# path: "/var/log/luet.log" # # Set logging level: error|warning|info|debug # level: "info" diff --git a/pkg/config/config.go b/pkg/config/config.go index 6557f0fb..3b2df64a 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -37,9 +37,10 @@ var LuetCfg = NewLuetConfig(v.GetViper()) var AvailableResolvers = strings.Join([]string{solver.QLearningResolverType}, " ") type LuetLoggingConfig struct { - Path string `mapstructure:"path"` - JsonFormat bool `mapstructure:"json_format"` - Level string `mapstructure:"level"` + Path string `mapstructure:"path"` + EnableLogFile bool `mapstructure:"enable_logfile"` + JsonFormat bool `mapstructure:"json_format"` + Level string `mapstructure:"level"` } type LuetGeneralConfig struct { @@ -205,7 +206,8 @@ func NewLuetConfig(viper *v.Viper) *LuetConfig { func GenDefault(viper *v.Viper) { viper.SetDefault("logging.level", "info") - viper.SetDefault("logging.path", "") + viper.SetDefault("logging.enable_logfile", false) + viper.SetDefault("logging.path", "/var/log/luet.log") viper.SetDefault("logging.json_format", false) viper.SetDefault("general.concurrency", runtime.NumCPU()) @@ -318,9 +320,10 @@ func (c *LuetLoggingConfig) SetLogLevel(s string) { func (c *LuetLoggingConfig) String() string { ans := fmt.Sprintf(` logging: + enable_logfile: %t path: %s json_format: %t - level: %s`, c.Path, c.JsonFormat, c.Level) + level: %s`, c.EnableLogFile, c.Path, c.JsonFormat, c.Level) return ans }