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