Added option for enable log to file.

Now it's possible logging to file is handled by the enable_logfile
option and by the path.

From cli is now possible:
* enable log to file with the option --enable-logfile
* modify the logfile path with the option --logfile/-l <path>
This commit is contained in:
Daniele Rondina 2020-05-07 08:14:37 +02:00
parent 993bcf9adf
commit fe5ab9246f
3 changed files with 21 additions and 9 deletions

View File

@ -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.

View File

@ -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"

View File

@ -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
}