From 03bb0e69d89b566ffa41c7a2813fdafe09b41fe0 Mon Sep 17 00:00:00 2001 From: John Olheiser Date: Sat, 16 Oct 2021 17:41:36 -0500 Subject: [PATCH] Replace debug with log-level flag (#440) * Add logging level to CLI and API Signed-off-by: jolheiser # Conflicts: # server/router/router.go * Apply suggestions from code review Co-authored-by: Anbraten * Refactor log level Signed-off-by: jolheiser * Remove API Signed-off-by: jolheiser * Fix API imports Signed-off-by: jolheiser * do not relay on external default Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Anbraten --- cmd/agent/agent.go | 23 +++++++++++++++++------ cmd/agent/flags.go | 5 +++++ cmd/server/flags.go | 5 +++++ cmd/server/server.go | 25 +++++++++++++++++-------- 4 files changed, 44 insertions(+), 14 deletions(-) diff --git a/cmd/agent/agent.go b/cmd/agent/agent.go index 507077953..faa1a1d7a 100644 --- a/cmd/agent/agent.go +++ b/cmd/agent/agent.go @@ -49,12 +49,6 @@ func loop(c *cli.Context) error { hostname, _ = os.Hostname() } - if c.BoolT("debug") { - zerolog.SetGlobalLevel(zerolog.DebugLevel) - } else { - zerolog.SetGlobalLevel(zerolog.WarnLevel) - } - if c.Bool("pretty") { log.Logger = log.Output( zerolog.ConsoleWriter{ @@ -64,6 +58,23 @@ func loop(c *cli.Context) error { ) } + zerolog.SetGlobalLevel(zerolog.WarnLevel) + if c.BoolT("debug") { + if c.IsSet("debug") { + log.Warn().Msg("--debug is deprecated, use --log-level instead") + } + zerolog.SetGlobalLevel(zerolog.DebugLevel) + } + + if c.IsSet("log-level") { + logLevelFlag := c.String("log-level") + lvl, err := zerolog.ParseLevel(logLevelFlag) + if err != nil { + log.Fatal().Msgf("unknown logging level: %s", logLevelFlag) + } + zerolog.SetGlobalLevel(lvl) + } + counter.Polling = c.Int("max-procs") counter.Running = 0 diff --git a/cmd/agent/flags.go b/cmd/agent/flags.go index 254b8d17d..d87d27952 100644 --- a/cmd/agent/flags.go +++ b/cmd/agent/flags.go @@ -43,6 +43,11 @@ var flags = []cli.Flag{ Name: "debug", Usage: "enable agent debug mode", }, + cli.StringFlag{ + EnvVar: "WOODPECKER_LOG_LEVEL", + Name: "log-level", + Usage: "set logging level", + }, cli.BoolFlag{ EnvVar: "WOODPECKER_DEBUG_PRETTY", Name: "pretty", diff --git a/cmd/server/flags.go b/cmd/server/flags.go index a0087f7c7..f9bb83b1b 100644 --- a/cmd/server/flags.go +++ b/cmd/server/flags.go @@ -26,6 +26,11 @@ var flags = []cli.Flag{ Name: "debug", Usage: "enable server debug mode", }, + cli.StringFlag{ + EnvVar: "WOODPECKER_LOG_LEVEL", + Name: "log-level", + Usage: "set logging level", + }, cli.BoolFlag{ EnvVar: "WOODPECKER_DEBUG_PRETTY", Name: "pretty", diff --git a/cmd/server/server.go b/cmd/server/server.go index 74ea3240d..3ae8abac5 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -52,14 +52,6 @@ import ( func loop(c *cli.Context) error { - // debug level if requested by user - // TODO: format output & options to switch to json aka. option to add channels to send logs to - if c.Bool("debug") { - zerolog.SetGlobalLevel(zerolog.DebugLevel) - } else { - zerolog.SetGlobalLevel(zerolog.WarnLevel) - } - if c.Bool("pretty") { log.Logger = log.Output( zerolog.ConsoleWriter{ @@ -69,6 +61,23 @@ func loop(c *cli.Context) error { ) } + // debug level if requested by user + // TODO: format output & options to switch to json aka. option to add channels to send logs to + zerolog.SetGlobalLevel(zerolog.WarnLevel) + if c.Bool("debug") { + log.Warn().Msg("--debug is deprecated, use --log-level instead") + zerolog.SetGlobalLevel(zerolog.DebugLevel) + } + + if c.IsSet("log-level") { + logLevelFlag := c.String("log-level") + lvl, err := zerolog.ParseLevel(logLevelFlag) + if err != nil { + log.Fatal().Msgf("unknown logging level: %s", logLevelFlag) + } + zerolog.SetGlobalLevel(lvl) + } + if c.String("server-host") == "" { log.Fatal().Msg("WOODPECKER_HOST is not properly configured") }