Skip to content

Commit

Permalink
Merge pull request #506 from warrensbox/505-split-output-and-logs
Browse files Browse the repository at this point in the history
Split output and logs
  • Loading branch information
warrensbox authored Sep 27, 2024
2 parents 31e43bc + ef79408 commit 97d40c8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/list_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,15 @@ func validMinorVersionFormat(version string) bool {
// ShowLatestVersion show install latest stable tf version
func ShowLatestVersion(mirrorURL string) {
tfversion, _ := getTFLatest(mirrorURL)
logger.Infof("%s", tfversion)
fmt.Printf("%s\n", tfversion)
}

// ShowLatestImplicitVersion show latest - argument (version) must be provided
func ShowLatestImplicitVersion(requestedVersion, mirrorURL string, preRelease bool) {
if validMinorVersionFormat(requestedVersion) {
tfversion, _ := getTFLatestImplicit(mirrorURL, preRelease, requestedVersion)
if len(tfversion) > 0 {
logger.Infof("%s", tfversion)
fmt.Printf("%s\n", tfversion)
} else {
logger.Fatal("The provided terraform version does not exist.\n Try `tfswitch -l` to see all available versions")
os.Exit(1)
Expand Down
30 changes: 24 additions & 6 deletions lib/logging.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package lib

import (
"os"

"github.com/gookit/color"
"github.com/gookit/slog"
"github.com/gookit/slog/handler"
"os"
)

var (
Expand All @@ -17,6 +19,22 @@ var (
TraceLogging = slog.Levels{slog.PanicLevel, slog.FatalLevel, slog.ErrorLevel, slog.WarnLevel, slog.InfoLevel, slog.NoticeLevel, slog.DebugLevel, slog.TraceLevel}
)

func NewStderrConsoleWithLF(lf slog.LevelFormattable) *handler.ConsoleHandler {
h := handler.NewIOWriterWithLF(os.Stderr, lf)

// default use text formatter
f := slog.NewTextFormatter()
// default enable color on console
f.WithEnableColor(color.SupportColor())

h.SetFormatter(f)
return h
}

func NewStderrConsoleHandler(levels []slog.Level) *handler.ConsoleHandler {
return NewStderrConsoleWithLF(slog.NewLvsFormatter(levels))
}

func InitLogger(logLevel string) *slog.Logger {
formatter := slog.NewTextFormatter()
formatter.EnableColor = true
Expand All @@ -26,23 +44,23 @@ func InitLogger(logLevel string) *slog.Logger {
var h *handler.ConsoleHandler
switch logLevel {
case "ERROR":
h = handler.NewConsoleHandler(ErrorLogging)
h = NewStderrConsoleHandler(ErrorLogging)
formatter.SetTemplate(loggingTemplateDebug)
break
case "TRACE":
h = handler.NewConsoleHandler(TraceLogging)
h = NewStderrConsoleHandler(TraceLogging)
formatter.SetTemplate(loggingTemplateDebug)
break
case "DEBUG":
h = handler.NewConsoleHandler(DebugLogging)
h = NewStderrConsoleHandler(DebugLogging)
formatter.SetTemplate(loggingTemplateDebug)
break
case "NOTICE":
h = handler.NewConsoleHandler(NoticeLogging)
h = NewStderrConsoleHandler(NoticeLogging)
formatter.SetTemplate(loggingTemplateDebug)
break
default:
h = handler.NewConsoleHandler(NormalLogging)
h = NewStderrConsoleHandler(NormalLogging)
formatter.SetTemplate(loggingTemplate)
}

Expand Down

0 comments on commit 97d40c8

Please sign in to comment.