From 9225a3a856e85f63e1160bff0acbd3cb21f3d9d4 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sat, 13 Aug 2022 13:12:38 +0800 Subject: [PATCH] chore(config): remove zerolog Signed-off-by: Bo-Yi Wu --- cmd/config.go | 18 +++++++++++++ cmd/config/config.go | 25 ------------------ cmd/root.go | 62 ++++++++++++++------------------------------ 3 files changed, 38 insertions(+), 67 deletions(-) create mode 100644 cmd/config.go delete mode 100644 cmd/config/config.go diff --git a/cmd/config.go b/cmd/config.go new file mode 100644 index 0000000..ec7249a --- /dev/null +++ b/cmd/config.go @@ -0,0 +1,18 @@ +package cmd + +import "github.com/kelseyhightower/envconfig" + +type ( + // Config provides the system configuration. + Config struct { + Debug bool `envconfig:"DRONE_DEBUG"` + Trace bool `envconfig:"DRONE_TRACE"` + } +) + +// fromEnviron returns the settings from the environment. +func fromEnviron() (Config, error) { + cfg := Config{} + err := envconfig.Process("", &cfg) + return cfg, err +} diff --git a/cmd/config/config.go b/cmd/config/config.go deleted file mode 100644 index 6625bea..0000000 --- a/cmd/config/config.go +++ /dev/null @@ -1,25 +0,0 @@ -package config - -import "github.com/kelseyhightower/envconfig" - -type ( - // Config provides the system configuration. - Config struct { - Logging Logging - } -) - -type Logging struct { - Debug bool `envconfig:"APP_LOGS_DEBUG"` - Level string `envconfig:"APP_LOGS_LEVEL" default:"info"` - NoColor bool `envconfig:"APP_LOGS_COLOR"` - Pretty bool `envconfig:"APP_LOGS_PRETTY"` - Text bool `envconfig:"APP_LOGS_TEXT"` -} - -// Environ returns the settings from the environment. -func Environ() (Config, error) { - cfg := Config{} - err := envconfig.Process("", &cfg) - return cfg, err -} diff --git a/cmd/root.go b/cmd/root.go index 29fc7b0..213f700 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -5,9 +5,6 @@ import ( "fmt" "os" "path/filepath" - "strings" - - "gitea.com/gitea/act_runner/cmd/config" "github.com/joho/godotenv" "github.com/mattn/go-isatty" @@ -15,9 +12,7 @@ import ( "github.com/nektos/act/pkg/common" "github.com/nektos/act/pkg/model" "github.com/nektos/act/pkg/runner" - "github.com/rs/zerolog" - "github.com/rs/zerolog/log" - "github.com/sirupsen/logrus" + log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -67,34 +62,19 @@ func (i *Input) newPlatforms() map[string]string { } } -// helper function cfgures the logging. -func initLogging(cfg config.Config) { +// initLogging setup the global logrus logger. +func initLogging(cfg Config) { isTerm := isatty.IsTerminal(os.Stdout.Fd()) + log.SetFormatter(&log.TextFormatter{ + DisableColors: !isTerm, + FullTimestamp: true, + }) - switch strings.ToLower(cfg.Logging.Level) { - case "panic": - zerolog.SetGlobalLevel(zerolog.PanicLevel) - case "fatal": - zerolog.SetGlobalLevel(zerolog.FatalLevel) - case "error": - zerolog.SetGlobalLevel(zerolog.ErrorLevel) - case "warn": - zerolog.SetGlobalLevel(zerolog.WarnLevel) - case "info": - zerolog.SetGlobalLevel(zerolog.InfoLevel) - case "debug": - zerolog.SetGlobalLevel(zerolog.DebugLevel) - default: - zerolog.SetGlobalLevel(zerolog.InfoLevel) + if cfg.Debug { + log.SetLevel(log.DebugLevel) } - - if cfg.Logging.Pretty || isTerm { - log.Logger = log.Output( - zerolog.ConsoleWriter{ - Out: os.Stderr, - NoColor: cfg.Logging.NoColor || !isTerm, - }, - ) + if cfg.Trace { + log.SetLevel(log.TraceLevel) } } @@ -127,11 +107,9 @@ func Execute(ctx context.Context) { rootCmd.PersistentFlags().StringVarP(&envfile, "env-file", "", ".env", "Read in a file of environment variables.") _ = godotenv.Load(envfile) - cfg, err := config.Environ() + cfg, err := fromEnviron() if err != nil { - log.Fatal(). - Err(err). - Msg("invalid cfguration") + log.Fatal("invalid cfguration") } initLogging(cfg) @@ -173,7 +151,7 @@ func runTask(ctx context.Context, input *Input, jobID string) error { if len(events) > 0 { // set default event type to first event // this way user dont have to specify the event. - log.Debug().Msgf("Using detected workflow event: %s", events[0]) + log.Debugf("Using detected workflow event: %s", events[0]) eventName = events[0] } else { if plan := planner.PlanEvent("push"); plan != nil { @@ -184,10 +162,10 @@ func runTask(ctx context.Context, input *Input, jobID string) error { // build the plan for this run var plan *model.Plan if jobID != "" { - log.Debug().Msgf("Planning job: %s", jobID) + log.Debugf("Planning job: %s", jobID) plan = planner.PlanJob(jobID) } else { - log.Debug().Msgf("Planning event: %s", eventName) + log.Debugf("Planning event: %s", eventName) plan = planner.PlanEvent(eventName) } @@ -246,14 +224,14 @@ func runTask(ctx context.Context, input *Input, jobID string) error { type taskLogHook struct{} -func (h *taskLogHook) Levels() []logrus.Level { - return logrus.AllLevels +func (h *taskLogHook) Levels() []log.Level { + return log.AllLevels } -func (h *taskLogHook) Fire(entry *logrus.Entry) error { +func (h *taskLogHook) Fire(entry *log.Entry) error { if flag, ok := entry.Data["raw_output"]; ok { if flagVal, ok := flag.(bool); flagVal && ok { - log.Info().Msgf("task log: %s", entry.Message) + log.Infof("task log: %s", entry.Message) } } return nil