Skip to content

Commit

Permalink
feat: add flag to change log level
Browse files Browse the repository at this point in the history
Signed-off-by: ludo <[email protected]>
  • Loading branch information
spiarh committed Jan 10, 2024
1 parent 1076941 commit 394efa6
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 9 deletions.
4 changes: 0 additions & 4 deletions cmd/container/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions cmd/simulator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,11 @@ bucket: <your-s3-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.
25 changes: 25 additions & 0 deletions internal/cli/simulator.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}
22 changes: 20 additions & 2 deletions internal/logging/factory.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 394efa6

Please sign in to comment.