Skip to content

Commit

Permalink
move to its own func
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Feb 15, 2024
1 parent c2e93af commit 15a0268
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions cmd/spawn/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,12 @@ func main() {
func GetLogger() *slog.Logger {
w := os.Stderr

logLevel, err := rootCmd.PersistentFlags().GetString(LogLevelFlag)
if err != nil {
log.Fatal(err)
}

var lvl slog.Level

switch strings.ToLower(logLevel) {
case "debug", "d":
lvl = slog.LevelDebug
case "info", "i":
lvl = slog.LevelInfo
case "warn", "w":
lvl = slog.LevelWarn
case "error", "e", "err":
lvl = slog.LevelError
default:
lvl = slog.LevelInfo
}
logLevel := parseLogLevelFromFlags()

slog.SetDefault(slog.New(
// TODO: Windows support colored logs: https://github.com/mattn/go-colorable `tint.NewHandler(colorable.NewColorable(w), nil)`
tint.NewHandler(w, &tint.Options{
Level: lvl,
Level: logLevel,
TimeFormat: time.Kitchen,
// Enables colors only if the terminal supports it
NoColor: !isatty.IsTerminal(w.Fd()),
Expand Down Expand Up @@ -87,3 +69,27 @@ var versionCmd = &cobra.Command{
fmt.Println(SpawnVersion)
},
}

func parseLogLevelFromFlags() slog.Level {
logLevel, err := rootCmd.PersistentFlags().GetString(LogLevelFlag)
if err != nil {
log.Fatal(err)
}

var lvl slog.Level

switch strings.ToLower(logLevel) {
case "debug", "d", "dbg":
lvl = slog.LevelDebug
case "info", "i", "inf":
lvl = slog.LevelInfo
case "warn", "w", "wrn":
lvl = slog.LevelWarn
case "error", "e", "err", "fatal", "f", "ftl":
lvl = slog.LevelError
default:
lvl = slog.LevelInfo
}

return lvl
}

0 comments on commit 15a0268

Please sign in to comment.