Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize logging with default info #648

Merged
merged 3 commits into from
May 23, 2018

Conversation

tejal29
Copy link
Contributor

@tejal29 tejal29 commented May 21, 2018

In c-s-t we are seeing errors https://github.com/GoogleContainerTools//issues/135

/bazel-bin/linux_amd64_pure_stripped/container_structure_test  --image gcr.io/google-appengine/debian8 --config tests/debian_failure_test.yaml  -verbose=debug

ERRO[0000] runtime error: invalid memory address or nil pointer dereference 
tejaldesai@tejaldesai~/go/src/github.com/GoogleContainerTools/container-structure-test: (fix_invalid_flags)$ 

This is because, we --image is not a valid arg for root command container_structure_test. The conbra.OnInitialize registered function initLogging is only called when the command is parsed correctly.
Hence, this diff initialize the logging with info log level.

@tejal29 tejal29 self-assigned this May 21, 2018
@tejal29 tejal29 requested a review from nkubala May 21, 2018 22:32
@tejal29
Copy link
Contributor Author

tejal29 commented May 21, 2018

On C-S-T after applying this diff.

tejaldesai@tejaldesai~/go/src/github.com/GoogleContainerTools/container-structure-test: (fix_invalid_flags)$ git diff
diff --git a/vendor/github.com/GoogleCloudPlatform/runtimes-common/ctc_lib/container_tool_command_base.go b/vendor/github.com/GoogleCloudPlatform/runtimes-common/ctc_lib/container_tool_command_base.go
index 8903986..9590039 100644
--- a/vendor/github.com/GoogleCloudPlatform/runtimes-common/ctc_lib/container_tool_command_base.go
+++ b/vendor/github.com/GoogleCloudPlatform/runtimes-common/ctc_lib/container_tool_command_base.go
@@ -24,6 +24,7 @@ import (
        "github.com/GoogleCloudPlatform/runtimes-common/ctc_lib/flags"
        "github.com/GoogleCloudPlatform/runtimes-common/ctc_lib/logging"
        "github.com/GoogleCloudPlatform/runtimes-common/ctc_lib/types"
+       log "github.com/sirupsen/logrus"
        "github.com/spf13/cobra"
        "github.com/spf13/viper"
 )
@@ -48,6 +49,15 @@ func (ctb *ContainerToolCommandBase) toolName() string {
 }
 
 func (ctb *ContainerToolCommandBase) Init() {
+       // Init Logging with info level with colors disabled since initLogging gets called
+       // only after arguments are pass.
+       Log = logging.NewLogger(
+               viper.GetString(config.LogDirConfigKey),
+               ctb.Name(),
+               log.InfoLevel,
+               false,
+       )
+
        cobra.OnInitialize(initConfig, ctb.initLogging)
        ctb.AddFlags()
        ctb.AddSubCommands()
tejaldesai@tejaldesai~/go/src/github.com/GoogleContainerTools/container-structure-test: (fix_invalid_flags)$

Run c-s-t

./bazel-bin/linux_amd64_pure_stripped/container_structure_test  --image gcr.io/google-appengine/debian8 --config tests/debian_failure_test.yaml  -verbose=debug

ERRO[0000] unknown flag: --image                        

Copy link
Contributor

@nkubala nkubala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tejal29 thanks for looking into this one. Can we also display the usage when someone provides an invalid flag? This will make it easier for them to figure out what flag they actually want, and save them the time of typing the help command.

@tejal29
Copy link
Contributor Author

tejal29 commented May 21, 2018

Working around the bug in cobra spf13/cobra#340 where RunE always displays usage.

Copy link
Contributor

@nkubala nkubala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one nit but otherwise LGTM

ctb.AddFlags()
ctb.AddSubCommands()
}

func (ctb *ContainerToolCommandBase) SetSilenceUsage() {
// Donot display usage when using RunE after args are parsed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: s/"Donot"/"Do not"

@tejal29 tejal29 merged commit 009842b into GoogleCloudPlatform:master May 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants