Skip to content

Commit 5f27728

Browse files
committed
cmd: allow log level to be set with an environment variable
This is not a config key because logging is used while loading the config.
1 parent ca85d41 commit 5f27728

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

cmd/root.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"context"
55
"fmt"
6+
"os"
67
"os/signal"
78
"path/filepath"
89
"syscall"
@@ -19,8 +20,6 @@ var (
1920
githash string
2021
)
2122

22-
var defaultLogLevel = log.InfoLevel.String()
23-
2423
var cfgFile string
2524
var logLevel string
2625
var logFormat string
@@ -44,9 +43,16 @@ and cloud-init for basic access configuration. This allows for fast cloning
4443
and resetting, for a stable test environment.`,
4544
}
4645

46+
defaultLogLevel := log.InfoLevel.String()
47+
48+
if envLogLevel, ok := os.LookupEnv("VIRTER_LOG_LEVEL"); ok {
49+
defaultLogLevel = envLogLevel
50+
}
51+
4752
configName := filepath.Join(configPath(), "virter.toml")
4853
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", fmt.Sprintf("config file (default is %v)", configName))
49-
rootCmd.PersistentFlags().StringVarP(&logLevel, "loglevel", "l", defaultLogLevel, "Log level")
54+
rootCmd.PersistentFlags().StringVarP(&logLevel, "loglevel", "l", defaultLogLevel,
55+
"Log level, default may be set with environment variable \"VIRTER_LOG_LEVEL\"")
5056
rootCmd.PersistentFlags().StringVar(&logFormat, "logformat", "default", "Log format, current options: short")
5157

5258
rootCmd.AddCommand(versionCommand())

0 commit comments

Comments
 (0)