From 394efa66a6059cf732e68c83bfc3fe8638b77b65 Mon Sep 17 00:00:00 2001 From: ludo Date: Mon, 8 Jan 2024 15:08:37 +0100 Subject: [PATCH] feat: add flag to change log level Signed-off-by: ludo --- cmd/container/main.go | 4 ---- cmd/simulator/main.go | 3 --- docs/cli.md | 8 ++++++++ internal/cli/simulator.go | 25 +++++++++++++++++++++++++ internal/logging/factory.go | 22 ++++++++++++++++++++-- 5 files changed, 53 insertions(+), 9 deletions(-) diff --git a/cmd/container/main.go b/cmd/container/main.go index b506ed2a..3571587f 100644 --- a/cmd/container/main.go +++ b/cmd/container/main.go @@ -10,7 +10,6 @@ import ( "github.com/controlplaneio/simulator/v2/core/tools" "github.com/controlplaneio/simulator/v2/internal/cli" "github.com/controlplaneio/simulator/v2/internal/config" - "github.com/controlplaneio/simulator/v2/internal/logging" ) func main() { @@ -21,9 +20,6 @@ func main() { terraformWorkspaceDir := filepath.Join(simulatorDir, "terraform/workspaces/simulator") ansiblePlaybookDir := filepath.Join(simulatorDir, "ansible/playbooks") - // configure slog - logging.Configure() - conf := config.Config{} if err := conf.Read(); err != nil { slog.Error("failed to read config", "error", err) diff --git a/cmd/simulator/main.go b/cmd/simulator/main.go index 3019032d..0cc9fb53 100644 --- a/cmd/simulator/main.go +++ b/cmd/simulator/main.go @@ -13,7 +13,6 @@ import ( "github.com/controlplaneio/simulator/v2/internal/cli" "github.com/controlplaneio/simulator/v2/internal/config" "github.com/controlplaneio/simulator/v2/internal/docker" - "github.com/controlplaneio/simulator/v2/internal/logging" ) const ( @@ -31,8 +30,6 @@ var ( ) func main() { - logging.Configure() - conf := config.Config{} if err := conf.Read(); err != nil { slog.Error("failed to read config", "error", err) diff --git a/docs/cli.md b/docs/cli.md index 96a6b9b3..e820afa4 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -60,3 +60,11 @@ bucket: container: image: controlplane/simulator:latest ``` + +## Log level + +The default log level is set to `error` by default. + +It can be changed using the `--log-level` flag or by setting +`SIMULATOR_LOG_LEVEL` environment variable to one of +error, warn, info or debug. diff --git a/internal/cli/simulator.go b/internal/cli/simulator.go index cebef0b1..32d64a55 100644 --- a/internal/cli/simulator.go +++ b/internal/cli/simulator.go @@ -1,9 +1,17 @@ package cli import ( + "os" + + "github.com/controlplaneio/simulator/v2/internal/logging" "github.com/spf13/cobra" ) +const ( + logLevelEnv = "SIMULATOR_LOG_LEVEL" + logLevelDefault = "error" +) + type SimulatorCmdOptions func(command *cobra.Command) func NewSimulatorCmd(opts ...SimulatorCmdOptions) *cobra.Command { @@ -12,9 +20,26 @@ func NewSimulatorCmd(opts ...SimulatorCmdOptions) *cobra.Command { Short: "Simulator CLI", } + simulator.PersistentFlags().String("log-level", logLevel(), "Log level (error, warn, info, debug)") + + simulator.PersistentPreRun = func(cmd *cobra.Command, _ []string) { + logLevel, err := cmd.Flags().GetString("log-level") + cobra.CheckErr(err) + + err = logging.Configure(logLevel) + cobra.CheckErr(err) + } + for _, opt := range opts { opt(simulator) } return simulator } + +func logLevel() string { + if l := os.Getenv(logLevelEnv); len(l) > 0 { + return l + } + return logLevelDefault +} diff --git a/internal/logging/factory.go b/internal/logging/factory.go index 3412dc18..2f544c74 100644 --- a/internal/logging/factory.go +++ b/internal/logging/factory.go @@ -1,16 +1,34 @@ package logging import ( + "fmt" "log/slog" "os" ) -func Configure() { +func Configure(level string) error { + var leveler slog.Leveler + + switch level { + case "error": + leveler = slog.LevelError + case "warn": + leveler = slog.LevelWarn + case "info": + leveler = slog.LevelInfo + case "debug": + leveler = slog.LevelDebug + default: + return fmt.Errorf("unsupported log level: " + level) + } + handlerOptions := &slog.HandlerOptions{ AddSource: true, - Level: slog.LevelError, + Level: leveler, } logger := slog.New(slog.NewTextHandler(os.Stdout, handlerOptions)) slog.SetDefault(logger) + + return nil }