Added optional -log-level flag to 'operator migrate' command (#15405)

This commit is contained in:
Peter Wilson 2022-05-12 20:56:25 +01:00 committed by GitHub
parent 63b31a13f9
commit 2939159a4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 5 deletions

3
changelog/15405.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:improvement
command: Support optional '-log-level' flag to be passed to 'operator migrate' command (defaults to info). Also support VAULT_LOG_LEVEL env var.
```

View File

@ -127,9 +127,9 @@ func (c *AgentCommand) Flags() *FlagSets {
Target: &c.flagLogLevel,
Default: "info",
EnvVar: "VAULT_LOG_LEVEL",
Completion: complete.PredictSet("trace", "debug", "info", "warn", "err"),
Completion: complete.PredictSet("trace", "debug", "info", "warn", "error"),
Usage: "Log verbosity level. Supported values (in order of detail) are " +
"\"trace\", \"debug\", \"info\", \"warn\", and \"err\".",
"\"trace\", \"debug\", \"info\", \"warn\", and \"error\".",
})
f.BoolVar(&BoolVar{

View File

@ -11,6 +11,7 @@ import (
"time"
log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-secure-stdlib/strutil"
"github.com/hashicorp/hcl"
"github.com/hashicorp/hcl/hcl/ast"
"github.com/hashicorp/vault/command/server"
@ -35,6 +36,7 @@ type OperatorMigrateCommand struct {
PhysicalBackends map[string]physical.Factory
flagConfig string
flagLogLevel string
flagStart string
flagReset bool
logger log.Logger
@ -96,6 +98,16 @@ func (c *OperatorMigrateCommand) Flags() *FlagSets {
Usage: "Reset the migration lock. No migration will occur.",
})
f.StringVar(&StringVar{
Name: "log-level",
Target: &c.flagLogLevel,
Default: "info",
EnvVar: "VAULT_LOG_LEVEL",
Completion: complete.PredictSet("trace", "debug", "info", "warn", "error"),
Usage: "Log verbosity level. Supported values (in order of detail) are " +
"\"trace\", \"debug\", \"info\", \"warn\", and \"error\". These are not case sensitive.",
})
return set
}
@ -108,7 +120,6 @@ func (c *OperatorMigrateCommand) AutocompleteFlags() complete.Flags {
}
func (c *OperatorMigrateCommand) Run(args []string) int {
c.logger = logging.NewVaultLogger(log.Info)
f := c.Flags()
if err := f.Parse(args); err != nil {
@ -116,6 +127,14 @@ func (c *OperatorMigrateCommand) Run(args []string) int {
return 1
}
c.flagLogLevel = strings.ToLower(c.flagLogLevel)
validLevels := []string{"trace", "debug", "info", "warn", "error"}
if !strutil.StrListContains(validLevels, c.flagLogLevel) {
c.UI.Error(fmt.Sprintf("%s is an unknown log level. Valid log levels are: %s", c.flagLogLevel, validLevels))
return 1
}
c.logger = logging.NewVaultLogger(log.LevelFromString(c.flagLogLevel))
if c.flagConfig == "" {
c.UI.Error("Must specify exactly one config path using -config")
return 1

View File

@ -189,9 +189,9 @@ func (c *ServerCommand) Flags() *FlagSets {
Target: &c.flagLogLevel,
Default: notSetValue,
EnvVar: "VAULT_LOG_LEVEL",
Completion: complete.PredictSet("trace", "debug", "info", "warn", "err"),
Completion: complete.PredictSet("trace", "debug", "info", "warn", "error"),
Usage: "Log verbosity level. Supported values (in order of detail) are " +
"\"trace\", \"debug\", \"info\", \"warn\", and \"err\".",
"\"trace\", \"debug\", \"info\", \"warn\", and \"error\".",
})
f.StringVar(&StringVar{