From 89fbf38f73c7c380d28045e7e3feb0ad7867769b Mon Sep 17 00:00:00 2001 From: Benjamin Chausse Date: Mon, 25 Nov 2024 23:51:08 -0500 Subject: fix: logs, readme, release version (#18) * Implement logsink instead of relying on /dev/null * Add version during build to `--help` * Update README roadmap --- internal/logging/logging.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 internal/logging/logging.go (limited to 'internal/logging/logging.go') diff --git a/internal/logging/logging.go b/internal/logging/logging.go new file mode 100644 index 0000000..71f0334 --- /dev/null +++ b/internal/logging/logging.go @@ -0,0 +1,35 @@ +package logging + +import ( + "log/slog" + "os" + + "github.com/ChausseBenjamin/termpicker/internal/util" +) + +type logSink struct{} + +func (l logSink) Write(p []byte) (n int, err error) { + return len(p), nil +} + +func Setup(filepath string) *os.File { + if filepath != "" { + logFile, err := os.Create(filepath) + if err != nil { + slog.Error("Failed to create log file", util.ErrKey, err.Error()) + os.Exit(1) + } + + handler := slog.NewJSONHandler(logFile, nil) + slog.SetDefault(slog.New(handler)) + + return logFile + } else { + // Since app is a TUI, logging to stdout/stderr would break the UI + // So we disable it by default + handler := slog.NewJSONHandler(logSink{}, nil) + slog.SetDefault(slog.New(handler)) + return nil + } +} -- cgit v1.2.3