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

Standardize logger #14

Merged
merged 1 commit into from
Oct 2, 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
13 changes: 12 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,26 @@ module github.com/simplifi/goverseer
go 1.22

require (
github.com/lmittmann/tint v1.0.4
github.com/charmbracelet/log v0.4.0
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/lipgloss v0.10.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/sys v0.13.0 // indirect
)
30 changes: 28 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,19 +1,45 @@
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/charmbracelet/lipgloss v0.10.0 h1:KWeXFSexGcfahHX+54URiZGkBFazf70JNMtwg/AFW3s=
github.com/charmbracelet/lipgloss v0.10.0/go.mod h1:Wig9DSfvANsxqkRsqj6x87irdy123SR4dOXlKa91ciE=
github.com/charmbracelet/log v0.4.0 h1:G9bQAcx8rWA2T3pWvx7YtPTPwgqpk7D68BX21IRW8ZM=
github.com/charmbracelet/log v0.4.0/go.mod h1:63bXt/djrizTec0l11H20t8FDSvA4CRZJ1KH22MdptM=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/lmittmann/tint v1.0.4 h1:LeYihpJ9hyGvE0w+K2okPTGUdVLfng1+nDNVR4vWISc=
github.com/lmittmann/tint v1.0.4/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
13 changes: 2 additions & 11 deletions internal/goverseer/executioner/executioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package executioner

import (
"fmt"
"log/slog"
"os"

"github.com/lmittmann/tint"
"github.com/simplifi/goverseer/internal/goverseer/config"
"github.com/simplifi/goverseer/internal/goverseer/executioner/log_executioner"
"github.com/simplifi/goverseer/internal/goverseer/executioner/shell_executioner"
Expand All @@ -20,17 +17,11 @@ type Executioner interface {
// New creates a new Executioner based on the config
// It returns an Executioner based on the config or an error
func New(cfg *config.Config) (Executioner, error) {
// Setup the logger
logger := slog.
New(tint.NewHandler(os.Stdout, nil)).
With("overseer", cfg.Name).
With("executioner", cfg.Executioner.Type)

switch cfg.Executioner.Type {
case "log":
return log_executioner.New(*cfg, logger)
return log_executioner.New(*cfg)
case "shell":
return shell_executioner.New(*cfg, logger)
return shell_executioner.New(*cfg)
default:
return nil, fmt.Errorf("unknown executioner type: %s", cfg.Executioner.Type)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package log_executioner

import (
"fmt"
"log/slog"
"os"

"github.com/charmbracelet/log"
"github.com/simplifi/goverseer/internal/goverseer/config"
)

Expand Down Expand Up @@ -43,32 +44,28 @@ type LogExecutioner struct {
Config

// log is the logger
log *slog.Logger
log *log.Logger
}

// New creates a new LogExecutioner based on the config
func New(cfg config.Config, log *slog.Logger) (*LogExecutioner, error) {
func New(cfg config.Config) (*LogExecutioner, error) {
lcfg, err := ParseConfig(cfg.Executioner.Config)
if err != nil {
return nil, err
}

if lcfg.Tag != "" {
log = log.With("tag", lcfg.Tag)
}

return &LogExecutioner{
log: log,
log: log.New(os.Stdout).With("tag", lcfg.Tag),
}, nil
}

// Execute logs the data to stdout
func (e *LogExecutioner) Execute(data interface{}) error {
e.log.Info("received data", slog.String("data", fmt.Sprintf("%v", data)))
e.log.Info("received data", "data", fmt.Sprintf("%v", data))
return nil
}

// Stop signals the executioner to stop
func (e *LogExecutioner) Stop() {
e.log.Info("shutting down executioner")
log.Info("shutting down executioner")
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package log_executioner

import (
"log/slog"
"os"
"testing"

"github.com/lmittmann/tint"
"github.com/simplifi/goverseer/internal/goverseer/config"
"github.com/stretchr/testify/assert"
)
Expand All @@ -30,7 +27,6 @@ func TestLogExecutioner_ParseConfig(t *testing.T) {

// TestLogExecutioner_Execute tests the Execute method of LogExecutioner
func TestLogExecutioner_Execute(t *testing.T) {
logger := slog.New(tint.NewHandler(os.Stderr, &tint.Options{Level: slog.LevelError}))
cfg := config.Config{
Name: "TestConfig",
Watcher: config.WatcherConfig{
Expand All @@ -44,7 +40,7 @@ func TestLogExecutioner_Execute(t *testing.T) {
},
}

executioner, err := New(cfg, logger)
executioner, err := New(cfg)
if err != nil {
t.Fatalf("Failed to create LogExecutioner: %v", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import (
"context"
"fmt"
"io"
"log/slog"
"os"
"os/exec"

"github.com/lmittmann/tint"
"github.com/charmbracelet/log"
"github.com/simplifi/goverseer/internal/goverseer/config"
)

Expand Down Expand Up @@ -79,9 +78,6 @@ type ShellExecutioner struct {
// the command to run and the data to pass into the command
workDir string

// log is the logger
log *slog.Logger

// stop is a channel to signal the executor to stop
stop chan struct{}

Expand All @@ -93,7 +89,7 @@ type ShellExecutioner struct {
}

// New creates a new ShellExecutioner based on the config
func New(cfg config.Config, log *slog.Logger) (*ShellExecutioner, error) {
func New(cfg config.Config) (*ShellExecutioner, error) {
pcfg, err := ParseConfig(cfg.Executioner.Config)
if err != nil {
return nil, fmt.Errorf("error parsing config: %w", err)
Expand All @@ -113,7 +109,6 @@ func New(cfg config.Config, log *slog.Logger) (*ShellExecutioner, error) {
Shell: pcfg.Shell,
},
workDir: workDir,
log: log,
stop: make(chan struct{}),
ctx: ctx,
cancel: cancel,
Expand All @@ -126,19 +121,19 @@ func (e *ShellExecutioner) streamOutput(pipe io.ReadCloser) {
for {
select {
case <-e.ctx.Done():
e.log.Info("stopping output scanner")
log.Info("stopping output scanner")
return
default:
if scanner.Scan() {
e.log.Info("command", slog.String("output", scanner.Text()))
log.Info("command", "output", scanner.Text())
} else {
if err := scanner.Err(); err != nil {
// Avoid logging errors if the context was canceled mid-scan
// This will happen when the executioner is being stopped
if e.ctx.Err() == context.Canceled {
continue
}
e.log.Error("error reading output", tint.Err(err))
log.Error("error reading output", "err", err)
}
return
}
Expand All @@ -153,7 +148,7 @@ func (e *ShellExecutioner) writeToWorkDir(name string, data interface{}) (string
if err := os.WriteFile(filePath, []byte(data.(string)), 0644); err != nil {
return "", fmt.Errorf("error writing file to work dir: %w", err)
}
e.log.Info("wrote file to work dir", slog.String("path", filePath))
log.Info("wrote file to work dir", "path", filePath)
return filePath, nil
}

Expand Down Expand Up @@ -227,6 +222,6 @@ func (e *ShellExecutioner) Execute(data interface{}) error {

// Stop signals the executioner to stop
func (e *ShellExecutioner) Stop() {
e.log.Info("shutting down executor")
log.Info("shutting down executor")
close(e.stop)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package shell_executioner

import (
"context"
"log/slog"
"os"
"testing"
"time"

"github.com/lmittmann/tint"
"github.com/simplifi/goverseer/internal/goverseer/config"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -101,7 +99,7 @@ func TestNew(t *testing.T) {
},
},
}
executioner, err := New(cfg, slog.Default())
executioner, err := New(cfg)
assert.NoError(t, err,
"Creating a new ShellExecutioner should not return an error")
assert.NotNil(t, executioner,
Expand All @@ -113,15 +111,14 @@ func TestNew(t *testing.T) {
Type: "shell",
},
}
executioner, err = New(cfg, slog.Default())
executioner, err = New(cfg)
assert.Error(t, err,
"Creating a new ShellExecutioner with an invalid config should return an error")
assert.Nil(t, executioner,
"Creating a new ShellExecutioner with an invalid config should not return an executioner")
}

func TestShellExecutioner_Execute(t *testing.T) {
log := slog.New(tint.NewHandler(os.Stderr, &tint.Options{Level: slog.LevelError}))
tempDir, _ := os.MkdirTemp("", "goverseer-test")
ctx, cancel := context.WithCancel(context.Background())
executioner := ShellExecutioner{
Expand All @@ -130,7 +127,6 @@ func TestShellExecutioner_Execute(t *testing.T) {
Shell: DefaultShell,
},
workDir: tempDir,
log: log,
stop: make(chan struct{}),
ctx: ctx,
cancel: cancel,
Expand All @@ -142,7 +138,6 @@ func TestShellExecutioner_Execute(t *testing.T) {
}

func TestShellExecutioner_Stop(t *testing.T) {
log := slog.New(tint.NewHandler(os.Stderr, &tint.Options{Level: slog.LevelError}))
tempDir, _ := os.MkdirTemp("", "goverseer-test")
ctx, cancel := context.WithCancel(context.Background())
executioner := ShellExecutioner{
Expand All @@ -151,7 +146,6 @@ func TestShellExecutioner_Stop(t *testing.T) {
Shell: DefaultShell,
},
workDir: tempDir,
log: log,
stop: make(chan struct{}),
ctx: ctx,
cancel: cancel,
Expand Down
21 changes: 5 additions & 16 deletions internal/goverseer/overseer/overseer.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package overseer

import (
"log/slog"
"os"
"sync"

"github.com/lmittmann/tint"
"github.com/charmbracelet/log"
"github.com/simplifi/goverseer/internal/goverseer/config"
"github.com/simplifi/goverseer/internal/goverseer/executioner"
"github.com/simplifi/goverseer/internal/goverseer/watcher"
Expand All @@ -21,9 +19,6 @@ type Overseer struct {
// executioner is the executioner
executioner executioner.Executioner

// log is the logger
log *slog.Logger

// change is the channel through which we send changes from the watcher to the executioner
change chan interface{}

Expand All @@ -36,11 +31,6 @@ type Overseer struct {

// New creates a new Overseer
func New(cfg *config.Config) (*Overseer, error) {
// Setup the logger
log := slog.
New(tint.NewHandler(os.Stdout, nil)).
With("overseer", cfg.Name)

watcher, err := watcher.New(cfg)
if err != nil {
return nil, err
Expand All @@ -54,7 +44,6 @@ func New(cfg *config.Config) (*Overseer, error) {
o := &Overseer{
watcher: watcher,
executioner: executioner,
log: log,
change: make(chan interface{}),
stop: make(chan struct{}),
}
Expand All @@ -80,7 +69,7 @@ func (o *Overseer) Run() {
go func() {
defer o.waitGroup.Done()
if err := o.executioner.Execute(data); err != nil {
o.log.Error("error running executioner", tint.Err(err))
log.Error("error running executioner", "err", err)
}
}()
}
Expand All @@ -89,14 +78,14 @@ func (o *Overseer) Run() {

// Stop signals the overseer to stop
func (o *Overseer) Stop() {
o.log.Info("shutting down overseer")
log.Info("shutting down overseer")
close(o.stop)
o.watcher.Stop()
o.executioner.Stop()

o.log.Info("waiting for overseer to finish")
log.Info("waiting for overseer to finish")
// Wait here so we don't close the changes channel before the executioner is done
o.waitGroup.Wait()
o.log.Info("done")
log.Info("done")
close(o.change)
}
Loading