From 971bc19cc178bdc9b7f1a916f61b265202792156 Mon Sep 17 00:00:00 2001 From: James Kwon Date: Thu, 24 Aug 2023 21:08:14 -0400 Subject: [PATCH] Fix log-level not working properly --- aws/resources/iam.go | 2 +- aws/resources/nat_gateway.go | 2 +- aws/resources/opensearch.go | 2 +- commands/cli.go | 5 +++-- logging/logger.go | 18 +++++++++++++----- 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/aws/resources/iam.go b/aws/resources/iam.go index efc71ba1..7eb87ccb 100644 --- a/aws/resources/iam.go +++ b/aws/resources/iam.go @@ -111,7 +111,7 @@ func (iu *IAMUsers) removeUserFromGroups(userName *string) error { func (iu *IAMUsers) deleteLoginProfile(userName *string) error { return retry.DoWithRetry( - logging.Logger, + logging.Logger.WithTime(time.Now()), "Delete Login Profile", 10, 2*time.Second, diff --git a/aws/resources/nat_gateway.go b/aws/resources/nat_gateway.go index 7c463e1b..9f4894b4 100644 --- a/aws/resources/nat_gateway.go +++ b/aws/resources/nat_gateway.go @@ -108,7 +108,7 @@ func (ngw *NatGateways) nukeAll(identifiers []*string) error { // Now wait until the NAT gateways are deleted err := retry.DoWithRetry( - logging.Logger, + logging.Logger.WithTime(time.Now()), "Waiting for all NAT gateways to be deleted.", // Wait a maximum of 5 minutes: 10 seconds in between, up to 30 times 30, 10*time.Second, diff --git a/aws/resources/opensearch.go b/aws/resources/opensearch.go index 63c2dc9b..120c4a17 100644 --- a/aws/resources/opensearch.go +++ b/aws/resources/opensearch.go @@ -180,7 +180,7 @@ func (osd *OpenSearchDomains) nukeAll(identifiers []*string) error { // Now wait until the OpenSearch Domains are deleted err := retry.DoWithRetry( - logging.Logger, + logging.Logger.WithTime(time.Now()), "Waiting for all OpenSearch Domains to be deleted.", // Wait a maximum of 5 minutes: 10 seconds in between, up to 30 times 30, 10*time.Second, diff --git a/commands/cli.go b/commands/cli.go index b1350457..9a13a56b 100644 --- a/commands/cli.go +++ b/commands/cli.go @@ -24,7 +24,7 @@ import ( // CreateCli - Create the CLI app with all commands, flags, and usage text configured. func CreateCli(version string) *cli.App { app := cli.NewApp() - logging.InitLogger("cloud-nuke", version) + logging.InitLogger() _, disableTelemetryFlag := os.LookupEnv("DISABLE_TELEMETRY") if !disableTelemetryFlag { ui.WarningMessage("This program sends telemetry to Gruntwork. To disable, set DISABLE_TELEMETRY=true as an environment variable") @@ -189,7 +189,8 @@ func parseLogLevel(c *cli.Context) error { if err != nil { return fmt.Errorf("Invalid log level - %s - %s", logLevel, err) } - logging.Logger.Level = parsedLogLevel + logging.Logger.SetLevel(parsedLogLevel) + logging.Logger.Debugf("Setting log level to %s", parsedLogLevel.String()) return nil } diff --git a/logging/logger.go b/logging/logger.go index 2d7f7dad..06fd0bd4 100644 --- a/logging/logger.go +++ b/logging/logger.go @@ -1,10 +1,18 @@ package logging -import "github.com/gruntwork-io/go-commons/logging" +import ( + "github.com/sirupsen/logrus" + "os" +) -// Logger - Global logger variable -var Logger = logging.GetLogger("cloud-nuke", "") +var Logger *logrus.Logger -func InitLogger(name string, version string) { - Logger = logging.GetLogger(name, version) +func InitLogger() { + Logger = logrus.New() + + // Set the desired log level (e.g., Debug, Info, Warn, Error, etc.) + Logger.SetLevel(logrus.InfoLevel) + + // You can also set the log output (e.g., os.Stdout, a file, etc.) + Logger.SetOutput(os.Stdout) }