Add log-level option to cli (#584)

* add log-level option to cli

* use app.Before

* rm not needed
This commit is contained in:
6543 2021-12-10 21:49:22 +01:00 committed by GitHub
parent e49f2e6061
commit f1e2f3dcad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 0 deletions

View file

@ -47,6 +47,11 @@ var GlobalFlags = []cli.Flag{
Usage: "socks proxy ignored", Usage: "socks proxy ignored",
Hidden: true, 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 // FormatFlag return format flag with value set based on template

23
cli/common/zerologger.go Normal file
View file

@ -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
}

View file

@ -63,6 +63,7 @@ func main() {
Out: os.Stderr, Out: os.Stderr,
}, },
) )
app.Before = common.SetupConsoleLogger
if err := app.Run(os.Args); err != nil { if err := app.Run(os.Args); err != nil {
zlog.Fatal().Err(err).Msg("error running cli") zlog.Fatal().Err(err).Msg("error running cli")