Skip to content

Commit

Permalink
feat: simplify logging configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Ric Featherstone authored and 06kellyjac committed Dec 21, 2023
1 parent 80be4d3 commit 08be674
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 26 deletions.
28 changes: 2 additions & 26 deletions controlplane/cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,11 @@
package main

import (
"log/slog"
"os"

"github.com/controlplaneio/simulator/controlplane/cli"
)

const (
LogLevel = "LOG_LEVEL"
"github.com/controlplaneio/simulator/logging"
)

func main() {
level, ok := os.LookupEnv(LogLevel)
if !ok {
level = "info"
}

var sLevel slog.Level

switch level {
case "debug":
sLevel = slog.LevelDebug
case "info":
sLevel = slog.LevelInfo
}

logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: sLevel,
}))
slog.SetDefault(logger)

logging.Configure()
cli.Execute()
}
2 changes: 2 additions & 0 deletions internal/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package main

import (
"github.com/controlplaneio/simulator/internal/cli"
"github.com/controlplaneio/simulator/logging"
)

func main() {
logging.Configure()
cli.Execute()
}
16 changes: 16 additions & 0 deletions logging/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package logging

import (
"log/slog"
"os"
)

func Configure() {
handlerOptions := &slog.HandlerOptions{
AddSource: true,
Level: slog.LevelError,
}

logger := slog.New(slog.NewJSONHandler(os.Stdout, handlerOptions))
slog.SetDefault(logger)
}

0 comments on commit 08be674

Please sign in to comment.