From f1e2f3dcad3c5eff9255f58cf8eb0430cd80880f Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 10 Dec 2021 21:49:22 +0100 Subject: [PATCH] Add log-level option to cli (#584) * add log-level option to cli * use app.Before * rm not needed --- cli/common/flags.go | 5 +++++ cli/common/zerologger.go | 23 +++++++++++++++++++++++ cmd/cli/main.go | 1 + 3 files changed, 29 insertions(+) create mode 100644 cli/common/zerologger.go diff --git a/cli/common/flags.go b/cli/common/flags.go index 9fbf469da..b58cfc6c1 100644 --- a/cli/common/flags.go +++ b/cli/common/flags.go @@ -47,6 +47,11 @@ var GlobalFlags = []cli.Flag{ Usage: "socks proxy ignored", Hidden: true, }, + &cli.StringFlag{ + EnvVars: []string{"WOODPECKER_LOG_LEVEL"}, + Name: "log-level", + Usage: "set logging level", + }, } // FormatFlag return format flag with value set based on template diff --git a/cli/common/zerologger.go b/cli/common/zerologger.go new file mode 100644 index 000000000..a1763c014 --- /dev/null +++ b/cli/common/zerologger.go @@ -0,0 +1,23 @@ +package common + +import ( + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" + "github.com/urfave/cli/v2" +) + +func SetupConsoleLogger(c *cli.Context) error { + if c.IsSet("log-level") { + level := c.String("log-level") + lvl, err := zerolog.ParseLevel(level) + if err != nil { + log.Fatal().Msgf("unknown logging level: %s", level) + } + zerolog.SetGlobalLevel(lvl) + } + if zerolog.GlobalLevel() <= zerolog.DebugLevel { + log.Logger = log.With().Caller().Logger() + log.Log().Msgf("LogLevel = %s", zerolog.GlobalLevel().String()) + } + return nil +} diff --git a/cmd/cli/main.go b/cmd/cli/main.go index a2db51ff6..e23277723 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -63,6 +63,7 @@ func main() { Out: os.Stderr, }, ) + app.Before = common.SetupConsoleLogger if err := app.Run(os.Args); err != nil { zlog.Fatal().Err(err).Msg("error running cli")