Skip to content

Commit

Permalink
Reapply "chore: move GetDebugLevel() for reuse"
Browse files Browse the repository at this point in the history
This reverts commit fa719e6.
  • Loading branch information
PeterSchafer committed Aug 1, 2024
1 parent fa62b65 commit 4e41ab4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
22 changes: 6 additions & 16 deletions cliv2/cmd/cliv2/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,12 @@ import (

"github.com/rs/zerolog"

"github.com/snyk/cli/cliv2/internal/utils"

"github.com/snyk/go-application-framework/pkg/configuration"
"github.com/snyk/go-application-framework/pkg/logging"
)

func getDebugLevel(config configuration.Configuration, logger *zerolog.Logger) zerolog.Level {
loglevel := zerolog.DebugLevel
if loglevelString := config.GetString("snyk_log_level"); loglevelString != "" {
var err error
loglevel, err = zerolog.ParseLevel(loglevelString)
if err == nil {
logger.Log().Msgf("Setting log level to %s", loglevelString)
} else {
logger.Log().Msgf("%v", err)
loglevel = zerolog.DebugLevel
}
}
return loglevel
}

func initDebugLogger(config configuration.Configuration) *zerolog.Logger {
debug := config.GetBool(configuration.DEBUG)
if !debug {
Expand All @@ -55,7 +42,10 @@ func initDebugLogger(config configuration.Configuration) *zerolog.Logger {

scrubLogger := logging.NewScrubbingWriter(zerolog.MultiLevelWriter(consoleWriter), logging.GetScrubDictFromConfig(config))
localLogger := zerolog.New(scrubLogger).With().Str("ext", "main").Str("separator", "-").Timestamp().Logger()
loglevel := getDebugLevel(config, &localLogger)

loglevel := utils.GetDebugLevel(config)
localLogger.Log().Msgf("Using log level: %s", loglevel)

debugLogger := localLogger.Level(loglevel)
return &debugLogger
}
Expand Down
1 change: 1 addition & 0 deletions cliv2/internal/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const SNYK_INTERNAL_PREVIEW_FEATURES_ENABLED = "SNYK_INTERNAL_PREVIEW_FEATURES"
const SNYK_ENDPOINT_ENV = "SNYK_API"
const SNYK_ORG_ENV = "SNYK_CFG_ORG"
const SNYK_OPENSSL_CONF = "OPENSSL_CONF"
const SNYK_LOG_LEVEL = "snyk_log_level"

const SNYK_HTTPS_PROXY_ENV_SYSTEM = "SNYK_SYSTEM_HTTPS_PROXY"
const SNYK_HTTP_PROXY_ENV_SYSTEM = "SNYK_SYSTEM_HTTP_PROXY"
Expand Down
20 changes: 20 additions & 0 deletions cliv2/internal/utils/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package utils

import (
"github.com/rs/zerolog"
"github.com/snyk/go-application-framework/pkg/configuration"

"github.com/snyk/cli/cliv2/internal/constants"
)

func GetDebugLevel(config configuration.Configuration) zerolog.Level {
loglevel := zerolog.DebugLevel
if loglevelString := config.GetString(constants.SNYK_LOG_LEVEL); loglevelString != "" {
var err error
loglevel, err = zerolog.ParseLevel(loglevelString)
if err != nil {
loglevel = zerolog.DebugLevel
}
}
return loglevel
}

0 comments on commit 4e41ab4

Please sign in to comment.