Skip to content

Commit

Permalink
feat: add logger (#179)
Browse files Browse the repository at this point in the history
Add the logger workflow
  • Loading branch information
alcb1310 authored Oct 14, 2024
1 parent a369e55 commit 39e5fab
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
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"
"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.
// 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

0 comments on commit 39e5fab

Please sign in to comment.