Skip to content

Commit 39e5fab

Browse files
authored
feat: add logger (#179)
Add the logger workflow
1 parent a369e55 commit 39e5fab

File tree

3 files changed

+82
-4
lines changed

3 files changed

+82
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ brew install joshmedeski/sesh/sesh
3838

3939
### ArchLinux AUR
4040

41-
To install sesh using, run the following [yay](https://aur.archlinux.org/packages/yay) command:
41+
To install sesh, run the following [yay](https://aur.archlinux.org/packages/yay) command:
4242

4343
```sh
4444
yay -S sesh-bin

main.go

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,89 @@
11
package main
22

33
import (
4-
"log"
4+
"fmt"
5+
"io" // This is used to enable the multiwritter and be able to write to the log file and the console at the same time
6+
"log/slog"
57
"os"
8+
"path" // This is used to create the path where the log files will be stored
9+
"strings" // This is required to conpare the evironment variables
10+
"time" // This is used to get the current date and create the log file
611

712
"github.com/joshmedeski/sesh/seshcli"
813
)
914

1015
var version = "dev"
1116

1217
func main() {
18+
slog.Debug("Debug")
19+
slog.Info("Information")
20+
slog.Warn("Warning")
21+
slog.Error("Error")
22+
1323
app := seshcli.App(version)
1424
if err := app.Run(os.Args); err != nil {
15-
log.Fatal(err)
25+
slog.Error("main file: ", "error", err)
26+
os.Exit(1)
27+
}
28+
}
29+
30+
func init() {
31+
var f *os.File
32+
var err error
33+
fileOnly := false
34+
35+
if f, err = createLoggerFile(); err != nil {
36+
slog.Error("Unable to create logger file", "error", err)
37+
os.Exit(1)
38+
}
39+
40+
env := os.Getenv("ENV")
41+
handlerOptions := &slog.HandlerOptions{}
42+
43+
switch strings.ToLower(env) {
44+
case "debug":
45+
handlerOptions.Level = slog.LevelDebug
46+
case "info":
47+
handlerOptions.Level = slog.LevelInfo
48+
case "error":
49+
handlerOptions.Level = slog.LevelError
50+
default:
51+
handlerOptions.Level = slog.LevelWarn
52+
fileOnly = true
53+
}
54+
55+
var loggerHandler *slog.JSONHandler
56+
if !fileOnly {
57+
multiWriter := io.MultiWriter(os.Stdout, f)
58+
loggerHandler = slog.NewJSONHandler(multiWriter, handlerOptions)
59+
} else {
60+
loggerHandler = slog.NewJSONHandler(f, handlerOptions)
1661
}
62+
slog.SetDefault(slog.New(loggerHandler))
63+
}
64+
65+
func createLoggerFile() (*os.File, error) {
66+
now := time.Now()
67+
date := fmt.Sprintf("%s.log", now.Format("2006-01-02"))
68+
69+
// TempDir returns the default directory to use for temporary files.
70+
//
71+
// On Unix systems, it returns $TMPDIR if non-empty, else /tmp.
72+
// On Windows, it uses GetTempPath, returning the first non-empty
73+
// value from %TMP%, %TEMP%, %USERPROFILE%, or the Windows directory.
74+
// On Plan 9, it returns /tmp.
75+
userTempDir := os.TempDir()
76+
slog.Debug("createLoggerFile:", "userTempDir", userTempDir)
77+
78+
if err := os.MkdirAll(path.Join(userTempDir, "sesh"), 0755); err != nil {
79+
return nil, err
80+
}
81+
82+
fileFullPath := path.Join(userTempDir, "sesh", date)
83+
file, err := os.OpenFile(fileFullPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
84+
if err != nil {
85+
return nil, err
86+
}
87+
88+
return file, nil
1789
}

seshcli/seshcli.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package seshcli
22

33
import (
4+
"log/slog"
5+
6+
"github.com/urfave/cli/v2"
7+
48
"github.com/joshmedeski/sesh/configurator"
59
"github.com/joshmedeski/sesh/connector"
610
"github.com/joshmedeski/sesh/dir"
@@ -19,7 +23,6 @@ import (
1923
"github.com/joshmedeski/sesh/tmux"
2024
"github.com/joshmedeski/sesh/tmuxinator"
2125
"github.com/joshmedeski/sesh/zoxide"
22-
"github.com/urfave/cli/v2"
2326
)
2427

2528
func App(version string) cli.App {
@@ -45,9 +48,12 @@ func App(version string) cli.App {
4548
config, err := configurator.NewConfigurator(os, path, runtime).GetConfig()
4649
// TODO: make sure to ignore the error if the config doesn't exist
4750
if err != nil {
51+
slog.Error("seshcli/seshcli.go: App", "error", err)
4852
panic(err)
4953
}
5054

55+
slog.Debug("seshcli/seshcli.go: App", "version", version, "config", config)
56+
5157
// core dependencies
5258
lister := lister.NewLister(config, home, tmux, zoxide, tmuxinator)
5359
startup := startup.NewStartup(config, lister, tmux)

0 commit comments

Comments
 (0)