summaryrefslogtreecommitdiff
path: root/internal/logging/logging.go
diff options
context:
space:
mode:
authorBenjamin Chausse <benjamin@chausse.xyz>2024-11-25 23:51:08 -0500
committerGitHub <noreply@github.com>2024-11-25 23:51:08 -0500
commit89fbf38f73c7c380d28045e7e3feb0ad7867769b (patch)
tree2a0b71e70b0949d1f1a70d371a2b5f29f61df2ba /internal/logging/logging.go
parent2b19466b38c69310a85d407343cd174ec6e1d0c3 (diff)
fix: logs, readme, release version (#18)
* Implement logsink instead of relying on /dev/null * Add version during build to `--help` * Update README roadmap
Diffstat (limited to 'internal/logging/logging.go')
-rw-r--r--internal/logging/logging.go35
1 files changed, 35 insertions, 0 deletions
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
+ }
+}