Skip to content

Commit

Permalink
Simplify the logging structure (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
abalaven authored Sep 27, 2024
1 parent 358c97c commit 3a92f6e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 23 deletions.
3 changes: 2 additions & 1 deletion chanGroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql/driver"
"sync"

"github.com/infobloxopen/hotload/logger"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
Expand Down Expand Up @@ -51,7 +52,7 @@ var _ = Describe("Driver", func() {
sqlDriver: nil,
mu: sync.RWMutex{},
conns: conns,
log: DefaultLogger{},
log: logger.DefaultLogger,
}
})

Expand Down
18 changes: 6 additions & 12 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,6 @@ type driverInstance struct {
options map[string]string
}

type DefaultLogger struct{}

func (d DefaultLogger) Debug(args ...any) {}
func (d DefaultLogger) Info(args ...any) {}
func (d DefaultLogger) Error(args ...any) {}

type driverOption func(*driverInstance)

// WithDriverOptions allows you to specify query parameters to the underlying driver.
Expand Down Expand Up @@ -206,15 +200,15 @@ func (cg *chanGroup) run() {
select {
case <-cg.parentCtx.Done():
cg.cancel()
cg.log.Debug("cancelling chanGroup context")
cg.log("cancelling chanGroup context")
return
case v := <-cg.values:
if v == cg.value {
// next update is the same, just ignore it
continue
}
cg.valueChanged(v)
cg.log.Debug("connection information changed")
cg.log("connection information changed")
}
}
}
Expand Down Expand Up @@ -293,11 +287,11 @@ func (cg *chanGroup) remove(conn *managedConn) {
func (cg *chanGroup) parseValues(vs url.Values) {
cg.mu.Lock()
defer cg.mu.Unlock()
cg.log.Debug("parsing values", vs)
cg.log("parsing values", vs)
if v, ok := vs[forceKill]; ok {
firstValue := v[0]
cg.forceKill = firstValue == "true"
cg.log.Debug("forceKill set to true")
cg.log("forceKill set to true")
}
}

Expand Down Expand Up @@ -346,13 +340,13 @@ func (h *hdriver) Open(name string) (driver.Conn, error) {
func WithLogger(l logger.Logger) {
log = l
if log == nil {
log = DefaultLogger{}
log = logger.DefaultLogger
}
}

func GetLogger() logger.Logger {
if log == nil {
return DefaultLogger{}
return logger.DefaultLogger
}
return log
}
10 changes: 5 additions & 5 deletions fsnotify/filewatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func readConfigFile(path string) (v []byte, err error) {
}

func resync(w watcher, pth string) (string, error) {
log.Debug("fsnotify: Path Name-Resync ", pth)
log("fsnotify: Path Name-Resync ", pth)
err := w.Remove(pth)
if err != nil && !errors.Is(err, rfsnotify.ErrNonExistentWatch) {
return "", err
Expand All @@ -69,7 +69,7 @@ func (s *Strategy) run() {
for {
select {
case e := <-s.watcher.GetEvents():
log.Debug("fsnotify: Path Name-Run ", e.Name)
log("fsnotify: Path Name-Run ", e.Name)
if e.Op != rfsnotify.Write && e.Op != rfsnotify.Remove {
continue
}
Expand All @@ -82,7 +82,7 @@ func (s *Strategy) run() {

s.setVal(e.Name, val)
case e := <-s.watcher.GetErrors():
log.Debug("got error: ", e)
log("got error: ", e)
break
case <-time.After(resyncPeriod):
var fixedPaths []string
Expand All @@ -104,7 +104,7 @@ func (s *Strategy) setVal(pth string, val string) {
s.mu.Lock()
defer s.mu.Unlock()
if _, ok := s.paths[pth]; !ok {
log.Debug("fsnotify: Path not in map ", pth)
log("fsnotify: Path not in map ", pth)
return
}
s.paths[pth].value = val
Expand All @@ -129,7 +129,7 @@ func (s *Strategy) Watch(ctx context.Context, pth string, options url.Values) (v
}
notifier, found := s.paths[pth]
if !found {
log.Debug("fsnotify: Path Name-Init ", pth)
log("fsnotify: Path Name-Init ", pth)
if err := s.watcher.Add(pth); err != nil {
return "", nil, err
}
Expand Down
9 changes: 4 additions & 5 deletions logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package logger

// Logger defines the interface for logging.
type Logger interface {
Info(args ...interface{})
Error(args ...interface{})
Debug(args ...interface{})
}
type Logger func(...interface{})

// DefaultLogger is a no-op logger function that does nothing
var DefaultLogger Logger = func(args ...interface{}) {}

0 comments on commit 3a92f6e

Please sign in to comment.