logging: permit to disable color and emoji

Now it's possible disable color and emoji on standard
output with:

$> luet <cmd> --color=false --emoji=false

Hereinafter, the list of changes:

* Added logging option logging.color (default true)
* Added logging option logging.enable_emoji (default true)
* Added persistent flag --color
* Added persistent flag --emoji
This commit is contained in:
Daniele Rondina
2020-05-09 10:08:21 +02:00
parent fe5ab9246f
commit 13df161fc6
5 changed files with 62 additions and 16 deletions

View File

@@ -88,10 +88,11 @@ func LoadConfig(c *config.LuetConfig) error {
return err
}
Debug("Using config file:", c.Viper.ConfigFileUsed())
InitAurora()
NewSpinner()
Debug("Using config file:", c.Viper.ConfigFileUsed())
if c.GetLogging().EnableLogFile && c.GetLogging().Path != "" {
// Init zap logger
err = ZapLogger()
@@ -153,6 +154,8 @@ func init() {
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.Bool("color", config.LuetCfg.GetLogging().Color, "Enable/Disable color.")
pflags.Bool("emoji", config.LuetCfg.GetLogging().EnableEmoji, "Enable/Disable emoji.")
pflags.StringP("logfile", "l", config.LuetCfg.GetLogging().Path,
"Logfile path. Empty value disable log to file.")
@@ -169,6 +172,8 @@ func init() {
pflags.Bool("same-owner", sameOwner, "Maintain same owner on uncompress.")
pflags.Int("concurrency", runtime.NumCPU(), "Concurrency")
config.LuetCfg.Viper.BindPFlag("logging.color", pflags.Lookup("color"))
config.LuetCfg.Viper.BindPFlag("logging.enable_emoji", pflags.Lookup("emoji"))
config.LuetCfg.Viper.BindPFlag("logging.enable_logfile", pflags.Lookup("enable-logfile"))
config.LuetCfg.Viper.BindPFlag("logging.path", pflags.Lookup("logfile"))

View File

@@ -37,10 +37,20 @@ var LuetCfg = NewLuetConfig(v.GetViper())
var AvailableResolvers = strings.Join([]string{solver.QLearningResolverType}, " ")
type LuetLoggingConfig struct {
Path string `mapstructure:"path"`
EnableLogFile bool `mapstructure:"enable_logfile"`
JsonFormat bool `mapstructure:"json_format"`
Level string `mapstructure:"level"`
// Path of the logfile
Path string `mapstructure:"path"`
// Enable/Disable logging to file
EnableLogFile bool `mapstructure:"enable_logfile"`
// Enable JSON format logging in file
JsonFormat bool `mapstructure:"json_format"`
// Log level
Level string `mapstructure:"level"`
// Enable emoji
EnableEmoji bool `mapstructure:"enable_emoji"`
// Enable/Disable color in logging
Color bool `mapstructure:"color"`
}
type LuetGeneralConfig struct {
@@ -209,6 +219,8 @@ func GenDefault(viper *v.Viper) {
viper.SetDefault("logging.enable_logfile", false)
viper.SetDefault("logging.path", "/var/log/luet.log")
viper.SetDefault("logging.json_format", false)
viper.SetDefault("logging.enable_emoji", true)
viper.SetDefault("logging.color", true)
viper.SetDefault("general.concurrency", runtime.NumCPU())
viper.SetDefault("general.debug", false)
@@ -323,7 +335,10 @@ logging:
enable_logfile: %t
path: %s
json_format: %t
level: %s`, c.EnableLogFile, c.Path, c.JsonFormat, c.Level)
color: %t
enable_emoji: %t
level: %s`, c.EnableLogFile, c.Path, c.JsonFormat,
c.Color, c.EnableEmoji, c.Level)
return ans
}

View File

@@ -54,6 +54,13 @@ var _ = Describe("Config", func() {
defer os.Remove(tmpFile.Name())
})
It("Config1", func() {
cfg := config.LuetCfg
cfg.GetLogging().Color = false
Expect(cfg.GetLogging().Color).To(BeFalse())
})
})
})

View File

@@ -35,7 +35,6 @@ import (
tree "github.com/mudler/luet/pkg/tree"
"github.com/ghodss/yaml"
. "github.com/logrusorgru/aurora"
"github.com/pkg/errors"
)
@@ -517,6 +516,7 @@ func (r *LuetSystemRepository) Client() Client {
func (r *LuetSystemRepository) Sync(force bool) (Repository, error) {
var repoUpdated bool = false
var treefs, metafs string
aurora := GetAurora()
Debug("Sync of the repository", r.Name, "in progress...")
c := r.Client()
@@ -648,9 +648,10 @@ func (r *LuetSystemRepository) Sync(force bool) (Repository, error) {
tsec, _ := strconv.ParseInt(repo.GetLastUpdate(), 10, 64)
InfoC(
Bold(Red(":house: Repository "+repo.GetName()+" revision: ")).String() +
Bold(Green(repo.GetRevision())).String() + " - " +
Bold(Green(time.Unix(tsec, 0).String())).String(),
aurora.Bold(
aurora.Red(":house: Repository "+repo.GetName()+" revision: ")).String() +
aurora.Bold(aurora.Green(repo.GetRevision())).String() + " - " +
aurora.Bold(aurora.Green(time.Unix(tsec, 0).String())).String(),
)
} else {
@@ -682,9 +683,10 @@ func (r *LuetSystemRepository) Sync(force bool) (Repository, error) {
repo.SetType(r.GetType())
repo.SetPriority(r.GetPriority())
InfoC(
Bold(Yellow(":information_source: Repository "+repo.GetName()+" priority: ")).String() +
Bold(Green(repo.GetPriority())).String() + " - type " +
Bold(Green(repo.GetType())).String(),
aurora.Bold(
aurora.Yellow(":information_source: Repository "+repo.GetName()+" priority: ")).String() +
aurora.Bold(aurora.Green(repo.GetPriority())).String() + " - type " +
aurora.Bold(aurora.Green(repo.GetType())).String(),
)
return repo, nil
}

View File

@@ -3,6 +3,7 @@ package logger
import (
"fmt"
"os"
"regexp"
. "github.com/mudler/luet/pkg/config"
@@ -15,6 +16,7 @@ import (
var s *spinner.Spinner = nil
var z *zap.Logger = nil
var aurora Aurora = nil
func NewSpinner() {
if s == nil {
@@ -24,6 +26,16 @@ func NewSpinner() {
}
}
func InitAurora() {
if aurora == nil {
aurora = NewAurora(LuetCfg.GetLogging().Color)
}
}
func GetAurora() Aurora {
return aurora
}
func ZapLogger() error {
var err error
if z == nil {
@@ -158,7 +170,7 @@ func msg(level string, withoutColor bool, msg ...interface{}) {
var levelMsg string
if withoutColor {
if withoutColor || !LuetCfg.GetLogging().Color {
levelMsg = message
} else {
switch level {
@@ -173,7 +185,12 @@ func msg(level string, withoutColor bool, msg ...interface{}) {
}
}
levelMsg = emoji.Sprint(levelMsg)
if LuetCfg.GetLogging().EnableEmoji {
levelMsg = emoji.Sprint(levelMsg)
} else {
re := regexp.MustCompile(`[:][\w]+[:]`)
levelMsg = re.ReplaceAllString(levelMsg, "")
}
if z != nil {
log2File(level, message)