Skip to content

Commit

Permalink
use slog instead of log.Printf
Browse files Browse the repository at this point in the history
  • Loading branch information
ktong committed Nov 11, 2023
1 parent 3e4299a commit 89f4d0d
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 25 deletions.
4 changes: 2 additions & 2 deletions provider/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package file
import (
"fmt"
"io/fs"
"log"
"log/slog"
"os"
)

Expand Down Expand Up @@ -47,7 +47,7 @@ func (f File) Load() (map[string]any, error) {
}
if err != nil {
if f.ignoreNotExist && os.IsNotExist(err) {
log.Printf("Config file %s does not exist.", f.path)
slog.Warn("Config file does not exist.", "file", f.path)

return make(map[string]any), nil
}
Expand Down
16 changes: 0 additions & 16 deletions provider/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
package file_test

import (
"bytes"
"context"
"errors"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -122,20 +120,6 @@ func TestFile_Load(t *testing.T) {
}
}

func TestFile_log(t *testing.T) {
buf := new(bytes.Buffer)
log.SetOutput(buf)
log.SetFlags(0)

_, err := file.New(
"not_found.json",
file.IgnoreFileNotExit(),
).Load()

require.NoError(t, err)
require.Equal(t, "Config file not_found.json does not exist.\n", buf.String())
}

func TestFile_Watch(t *testing.T) {
testcases := []struct {
description string
Expand Down
10 changes: 5 additions & 5 deletions provider/file/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package file
import (
"context"
"fmt"
"log"
"log/slog"
"path/filepath"
"time"

Expand All @@ -26,7 +26,7 @@ func (f File) Watch(ctx context.Context, watchFunc func(map[string]any)) error {
}
defer func() {
if err := watcher.Close(); err != nil {
log.Printf("Error when closing watcher for %s: %v", f.path, err)
slog.Error("Error when closing file watcher.", "file", f.path, "error", err)

Check warning on line 29 in provider/file/watch.go

View check run for this annotation

Codecov / codecov/patch

provider/file/watch.go#L29

Added line #L29 was not covered by tests
}
}()

Expand Down Expand Up @@ -73,12 +73,12 @@ func (f File) Watch(ctx context.Context, watchFunc func(map[string]any)) error {

switch {
case event.Has(fsnotify.Remove):
log.Printf("Config file %s has been removed.", f.path)
slog.Warn("Config file has been removed.", "file", f.path)
watchFunc(nil)
case event.Has(fsnotify.Create) || event.Has(fsnotify.Write):
values, err := f.Load()
if err != nil {
log.Printf("Error when reloading configuration from %s: %v", f.path, err)
slog.Error("Error when reloading config file", "file", f.path, "error", err)

Check warning on line 81 in provider/file/watch.go

View check run for this annotation

Codecov / codecov/patch

provider/file/watch.go#L81

Added line #L81 was not covered by tests

continue
}
Expand All @@ -90,7 +90,7 @@ func (f File) Watch(ctx context.Context, watchFunc func(map[string]any)) error {
return nil
}

log.Printf("Error when watching file %s: %v", f.path, err)
slog.Error("Error when watching file", "file", f.path, "error", err)

Check warning on line 93 in provider/file/watch.go

View check run for this annotation

Codecov / codecov/patch

provider/file/watch.go#L93

Added line #L93 was not covered by tests

case <-ctx.Done():
return nil
Expand Down
4 changes: 2 additions & 2 deletions provider/file/watch_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ package file

import (
"context"
"log"
"log/slog"
"runtime"
)

func (f File) Watch(context.Context, func(map[string]any)) error {
log.Printf("File.Watch does not supported on %s.", runtime.GOOS)
slog.Warn("File.Watch does not supported on runtime.", "runtime", runtime.GOOS)

return nil
}

0 comments on commit 89f4d0d

Please sign in to comment.