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
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ctc_lib/container_tool_command_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -50,6 +51,14 @@ 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 parsed correctly.
Log = logging.NewLogger(
viper.GetString(config.LogDirConfigKey),
ctb.Name(),
log.InfoLevel,
false,
)
cobra.OnInitialize(initConfig, ctb.initLogging)
ctb.AddFlags()
ctb.AddSubCommands()
Expand Down
28 changes: 28 additions & 0 deletions ctc_lib/container_tool_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,31 @@ func TestContainerToolCommandOutputInJson(t *testing.T) {
t.Errorf("Expected to contain: \n %v\nGot:\n %v\n", expectedObj, actualObj)
}
}

func TestContainerToolCommandLogNotNull(t *testing.T) {
defer SetExitOnError(true)
testCommand := ContainerToolCommand{
ContainerToolCommandBase: &ContainerToolCommandBase{
Command: &cobra.Command{
Use: "loggingError",
Short: "Logging not nil",
},
},
Output: "",
RunO: func(command *cobra.Command, args []string) (interface{}, error) {
return nil, nil
},
}
SetExitOnError(false)
var OutputBuffer bytes.Buffer
testCommand.Command.SetOutput(&OutputBuffer)
testCommand.SetArgs([]string{"--name=Sparks"})
err := ExecuteE(&testCommand)
expectedError := ("unknown flag: --name")
if Log == nil {
t.Errorf("Expected Log to be not nil. Got nil")
}
if err.Error() != expectedError {
t.Errorf("Expected Error: \n %q \nGot:\n %q\n", expectedError, err.Error())
}
}