diff --git a/pkg/kando/kando.go b/pkg/kando/kando.go index cc6f2b257c..755085db5c 100644 --- a/pkg/kando/kando.go +++ b/pkg/kando/kando.go @@ -25,6 +25,8 @@ import ( "github.com/kanisterio/kanister/pkg/version" ) +var logLevel string + // Execute adds all child commands to the root command and sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { @@ -43,10 +45,9 @@ func newRootCommand() *cobra.Command { Version: version.VersionString(), } - var v string - rootCmd.PersistentFlags().StringVarP(&v, "verbosity", "v", logrus.WarnLevel.String(), "Log level (debug, info, warn, error, fatal, panic)") + rootCmd.PersistentFlags().StringVarP(&logLevel, "verbosity", "v", logrus.WarnLevel.String(), "Log level (debug, info, warn, error, fatal, panic)") rootCmd.PersistentPreRunE = func(*cobra.Command, []string) error { - return setLogLevel(v) + return setLogLevel(logLevel) } rootCmd.AddCommand(newLocationCommand()) diff --git a/pkg/kando/process.go b/pkg/kando/process.go index fa41d8d442..f259696def 100644 --- a/pkg/kando/process.go +++ b/pkg/kando/process.go @@ -15,23 +15,56 @@ package kando import ( + "io" "net" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "google.golang.org/grpc/grpclog" ) const ( processAddressFlagName = "address" ) +// grpclogLogger method taken from grpclog/loggerv2.go:newLoggerV2 +// see also logging/logrus/grpclogger.go. +// grpclogger.go may be the best way to setup the loggers +// but kanister logging, while using logrus, does not seem to offer a straightforward +// interface for performing the interfacing in the grpclogger.go example. +func grpclogLogger(cmd *cobra.Command) grpclog.LoggerV2 { + var infow, warnw, errorw io.Writer + infow = io.Discard + warnw = io.Discard + errorw = io.Discard + switch { + case logrus.IsLevelEnabled(logrus.InfoLevel): + infow = cmd.ErrOrStderr() + case logrus.IsLevelEnabled(logrus.WarnLevel): + warnw = cmd.ErrOrStderr() + case logrus.IsLevelEnabled(logrus.ErrorLevel): + errorw = cmd.ErrOrStderr() + } + return grpclog.NewLoggerV2(infow, warnw, errorw) +} + func newProcessCommand() *cobra.Command { cmd := &cobra.Command{ Use: "process ", Short: "Manage kando processes", } + cmd.AddCommand(newProcessServerCommand()) cmd.AddCommand(newProcessClientCommand()) cmd.PersistentFlags().StringP(processAddressFlagName, "a", "/tmp/kanister.sock", "The path of a unix socket of the process server") + cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { + err := setLogLevel(logLevel) + if err != nil { + return err + } + grpclog.SetLoggerV2(grpclogLogger(cmd)) + return nil + } return cmd } diff --git a/pkg/kando/process_client_signal.go b/pkg/kando/process_client_signal.go index da727c4c1b..785838b6bf 100644 --- a/pkg/kando/process_client_signal.go +++ b/pkg/kando/process_client_signal.go @@ -27,7 +27,7 @@ import ( func newProcessClientSignalCommand() *cobra.Command { cmd := &cobra.Command{ - Use: "signal SIGNAL PID", + Use: "signal PID SIGNAL", Short: "send a signal to a managed process", Args: cobra.ExactArgs(2), RunE: runProcessClientSignal,