mirror of
https://github.com/mudler/luet.git
synced 2025-09-19 17:54:29 +00:00
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:
@@ -88,10 +88,11 @@ func LoadConfig(c *config.LuetConfig) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
Debug("Using config file:", c.Viper.ConfigFileUsed())
|
InitAurora()
|
||||||
|
|
||||||
NewSpinner()
|
NewSpinner()
|
||||||
|
|
||||||
|
Debug("Using config file:", c.Viper.ConfigFileUsed())
|
||||||
|
|
||||||
if c.GetLogging().EnableLogFile && c.GetLogging().Path != "" {
|
if c.GetLogging().EnableLogFile && c.GetLogging().Path != "" {
|
||||||
// Init zap logger
|
// Init zap logger
|
||||||
err = ZapLogger()
|
err = ZapLogger()
|
||||||
@@ -153,6 +154,8 @@ func init() {
|
|||||||
pflags.BoolP("debug", "d", false, "verbose output")
|
pflags.BoolP("debug", "d", false, "verbose output")
|
||||||
pflags.Bool("fatal", false, "Enables Warnings to exit")
|
pflags.Bool("fatal", false, "Enables Warnings to exit")
|
||||||
pflags.Bool("enable-logfile", false, "Enable log to file")
|
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,
|
pflags.StringP("logfile", "l", config.LuetCfg.GetLogging().Path,
|
||||||
"Logfile path. Empty value disable log to file.")
|
"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.Bool("same-owner", sameOwner, "Maintain same owner on uncompress.")
|
||||||
pflags.Int("concurrency", runtime.NumCPU(), "Concurrency")
|
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.enable_logfile", pflags.Lookup("enable-logfile"))
|
||||||
config.LuetCfg.Viper.BindPFlag("logging.path", pflags.Lookup("logfile"))
|
config.LuetCfg.Viper.BindPFlag("logging.path", pflags.Lookup("logfile"))
|
||||||
|
|
||||||
|
@@ -37,10 +37,20 @@ var LuetCfg = NewLuetConfig(v.GetViper())
|
|||||||
var AvailableResolvers = strings.Join([]string{solver.QLearningResolverType}, " ")
|
var AvailableResolvers = strings.Join([]string{solver.QLearningResolverType}, " ")
|
||||||
|
|
||||||
type LuetLoggingConfig struct {
|
type LuetLoggingConfig struct {
|
||||||
|
// Path of the logfile
|
||||||
Path string `mapstructure:"path"`
|
Path string `mapstructure:"path"`
|
||||||
|
// Enable/Disable logging to file
|
||||||
EnableLogFile bool `mapstructure:"enable_logfile"`
|
EnableLogFile bool `mapstructure:"enable_logfile"`
|
||||||
|
// Enable JSON format logging in file
|
||||||
JsonFormat bool `mapstructure:"json_format"`
|
JsonFormat bool `mapstructure:"json_format"`
|
||||||
|
|
||||||
|
// Log level
|
||||||
Level string `mapstructure:"level"`
|
Level string `mapstructure:"level"`
|
||||||
|
|
||||||
|
// Enable emoji
|
||||||
|
EnableEmoji bool `mapstructure:"enable_emoji"`
|
||||||
|
// Enable/Disable color in logging
|
||||||
|
Color bool `mapstructure:"color"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type LuetGeneralConfig struct {
|
type LuetGeneralConfig struct {
|
||||||
@@ -209,6 +219,8 @@ func GenDefault(viper *v.Viper) {
|
|||||||
viper.SetDefault("logging.enable_logfile", false)
|
viper.SetDefault("logging.enable_logfile", false)
|
||||||
viper.SetDefault("logging.path", "/var/log/luet.log")
|
viper.SetDefault("logging.path", "/var/log/luet.log")
|
||||||
viper.SetDefault("logging.json_format", false)
|
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.concurrency", runtime.NumCPU())
|
||||||
viper.SetDefault("general.debug", false)
|
viper.SetDefault("general.debug", false)
|
||||||
@@ -323,7 +335,10 @@ logging:
|
|||||||
enable_logfile: %t
|
enable_logfile: %t
|
||||||
path: %s
|
path: %s
|
||||||
json_format: %t
|
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
|
return ans
|
||||||
}
|
}
|
||||||
|
@@ -54,6 +54,13 @@ var _ = Describe("Config", func() {
|
|||||||
defer os.Remove(tmpFile.Name())
|
defer os.Remove(tmpFile.Name())
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("Config1", func() {
|
||||||
|
cfg := config.LuetCfg
|
||||||
|
|
||||||
|
cfg.GetLogging().Color = false
|
||||||
|
Expect(cfg.GetLogging().Color).To(BeFalse())
|
||||||
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
@@ -35,7 +35,6 @@ import (
|
|||||||
tree "github.com/mudler/luet/pkg/tree"
|
tree "github.com/mudler/luet/pkg/tree"
|
||||||
|
|
||||||
"github.com/ghodss/yaml"
|
"github.com/ghodss/yaml"
|
||||||
. "github.com/logrusorgru/aurora"
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -517,6 +516,7 @@ func (r *LuetSystemRepository) Client() Client {
|
|||||||
func (r *LuetSystemRepository) Sync(force bool) (Repository, error) {
|
func (r *LuetSystemRepository) Sync(force bool) (Repository, error) {
|
||||||
var repoUpdated bool = false
|
var repoUpdated bool = false
|
||||||
var treefs, metafs string
|
var treefs, metafs string
|
||||||
|
aurora := GetAurora()
|
||||||
|
|
||||||
Debug("Sync of the repository", r.Name, "in progress...")
|
Debug("Sync of the repository", r.Name, "in progress...")
|
||||||
c := r.Client()
|
c := r.Client()
|
||||||
@@ -648,9 +648,10 @@ func (r *LuetSystemRepository) Sync(force bool) (Repository, error) {
|
|||||||
tsec, _ := strconv.ParseInt(repo.GetLastUpdate(), 10, 64)
|
tsec, _ := strconv.ParseInt(repo.GetLastUpdate(), 10, 64)
|
||||||
|
|
||||||
InfoC(
|
InfoC(
|
||||||
Bold(Red(":house: Repository "+repo.GetName()+" revision: ")).String() +
|
aurora.Bold(
|
||||||
Bold(Green(repo.GetRevision())).String() + " - " +
|
aurora.Red(":house: Repository "+repo.GetName()+" revision: ")).String() +
|
||||||
Bold(Green(time.Unix(tsec, 0).String())).String(),
|
aurora.Bold(aurora.Green(repo.GetRevision())).String() + " - " +
|
||||||
|
aurora.Bold(aurora.Green(time.Unix(tsec, 0).String())).String(),
|
||||||
)
|
)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@@ -682,9 +683,10 @@ func (r *LuetSystemRepository) Sync(force bool) (Repository, error) {
|
|||||||
repo.SetType(r.GetType())
|
repo.SetType(r.GetType())
|
||||||
repo.SetPriority(r.GetPriority())
|
repo.SetPriority(r.GetPriority())
|
||||||
InfoC(
|
InfoC(
|
||||||
Bold(Yellow(":information_source: Repository "+repo.GetName()+" priority: ")).String() +
|
aurora.Bold(
|
||||||
Bold(Green(repo.GetPriority())).String() + " - type " +
|
aurora.Yellow(":information_source: Repository "+repo.GetName()+" priority: ")).String() +
|
||||||
Bold(Green(repo.GetType())).String(),
|
aurora.Bold(aurora.Green(repo.GetPriority())).String() + " - type " +
|
||||||
|
aurora.Bold(aurora.Green(repo.GetType())).String(),
|
||||||
)
|
)
|
||||||
return repo, nil
|
return repo, nil
|
||||||
}
|
}
|
||||||
|
@@ -3,6 +3,7 @@ package logger
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
|
|
||||||
. "github.com/mudler/luet/pkg/config"
|
. "github.com/mudler/luet/pkg/config"
|
||||||
|
|
||||||
@@ -15,6 +16,7 @@ import (
|
|||||||
|
|
||||||
var s *spinner.Spinner = nil
|
var s *spinner.Spinner = nil
|
||||||
var z *zap.Logger = nil
|
var z *zap.Logger = nil
|
||||||
|
var aurora Aurora = nil
|
||||||
|
|
||||||
func NewSpinner() {
|
func NewSpinner() {
|
||||||
if s == nil {
|
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 {
|
func ZapLogger() error {
|
||||||
var err error
|
var err error
|
||||||
if z == nil {
|
if z == nil {
|
||||||
@@ -158,7 +170,7 @@ func msg(level string, withoutColor bool, msg ...interface{}) {
|
|||||||
|
|
||||||
var levelMsg string
|
var levelMsg string
|
||||||
|
|
||||||
if withoutColor {
|
if withoutColor || !LuetCfg.GetLogging().Color {
|
||||||
levelMsg = message
|
levelMsg = message
|
||||||
} else {
|
} else {
|
||||||
switch level {
|
switch level {
|
||||||
@@ -173,7 +185,12 @@ func msg(level string, withoutColor bool, msg ...interface{}) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if LuetCfg.GetLogging().EnableEmoji {
|
||||||
levelMsg = emoji.Sprint(levelMsg)
|
levelMsg = emoji.Sprint(levelMsg)
|
||||||
|
} else {
|
||||||
|
re := regexp.MustCompile(`[:][\w]+[:]`)
|
||||||
|
levelMsg = re.ReplaceAllString(levelMsg, "")
|
||||||
|
}
|
||||||
|
|
||||||
if z != nil {
|
if z != nil {
|
||||||
log2File(level, message)
|
log2File(level, message)
|
||||||
|
Reference in New Issue
Block a user