Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add logger #179

Merged
merged 7 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ brew install joshmedeski/sesh/sesh

### ArchLinux AUR

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

```sh
yay -S sesh-bin
Expand Down
76 changes: 74 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,89 @@
package main

import (
"log"
"fmt"
alcb1310 marked this conversation as resolved.
Show resolved Hide resolved
"io" // This is used to enable the multiwritter and be able to write to the log file and the console at the same time
"log/slog"
"os"
"path" // This is used to create the path where the log files will be stored
"strings" // This is required to conpare the evironment variables
"time" // This is used to get the current date and create the log file

"github.com/joshmedeski/sesh/seshcli"
)

var version = "dev"

func main() {
slog.Debug("Debug")
slog.Info("Information")
slog.Warn("Warning")
slog.Error("Error")

app := seshcli.App(version)
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
slog.Error("main file: ", "error", err)
os.Exit(1)
}
}

func init() {
var f *os.File
var err error
fileOnly := false

if f, err = createLoggerFile(); err != nil {
slog.Error("Unable to create logger file", "error", err)
os.Exit(1)
}

env := os.Getenv("ENV")
handlerOptions := &slog.HandlerOptions{}

switch strings.ToLower(env) {
case "debug":
handlerOptions.Level = slog.LevelDebug
case "info":
handlerOptions.Level = slog.LevelInfo
case "error":
handlerOptions.Level = slog.LevelError
default:
handlerOptions.Level = slog.LevelWarn
fileOnly = true
}

var loggerHandler *slog.JSONHandler
if !fileOnly {
multiWriter := io.MultiWriter(os.Stdout, f)
loggerHandler = slog.NewJSONHandler(multiWriter, handlerOptions)
} else {
loggerHandler = slog.NewJSONHandler(f, handlerOptions)
}
slog.SetDefault(slog.New(loggerHandler))
}

func createLoggerFile() (*os.File, error) {
now := time.Now()
date := fmt.Sprintf("%s.log", now.Format("2006-01-02"))

// TempDir returns the default directory to use for temporary files.
//
// On Unix systems, it returns $TMPDIR if non-empty, else /tmp.
alcb1310 marked this conversation as resolved.
Show resolved Hide resolved
// On Windows, it uses GetTempPath, returning the first non-empty
// value from %TMP%, %TEMP%, %USERPROFILE%, or the Windows directory.
// On Plan 9, it returns /tmp.
userTempDir := os.TempDir()
slog.Debug("createLoggerFile:", "userTempDir", userTempDir)

if err := os.MkdirAll(path.Join(userTempDir, "sesh"), 0755); err != nil {
return nil, err
}

fileFullPath := path.Join(userTempDir, "sesh", date)
file, err := os.OpenFile(fileFullPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return nil, err
}

return file, nil
}
8 changes: 7 additions & 1 deletion seshcli/seshcli.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package seshcli

import (
"log/slog"

"github.com/urfave/cli/v2"

"github.com/joshmedeski/sesh/configurator"
"github.com/joshmedeski/sesh/connector"
"github.com/joshmedeski/sesh/dir"
Expand All @@ -19,7 +23,6 @@ import (
"github.com/joshmedeski/sesh/tmux"
"github.com/joshmedeski/sesh/tmuxinator"
"github.com/joshmedeski/sesh/zoxide"
"github.com/urfave/cli/v2"
)

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

slog.Debug("seshcli/seshcli.go: App", "version", version, "config", config)

// core dependencies
lister := lister.NewLister(config, home, tmux, zoxide, tmuxinator)
startup := startup.NewStartup(config, lister, tmux)
Expand Down