From cdf52037a8fc49c1f0a01b593e67c5f47806cc2d Mon Sep 17 00:00:00 2001 From: Ankhit Bala Venkata Date: Thu, 26 Sep 2024 17:19:40 -0700 Subject: [PATCH] Simplify the logging structure --- chanGroup_test.go | 3 ++- driver.go | 18 ++++++------------ fsnotify/filewatcher.go | 10 +++++----- logger/logger.go | 9 ++++----- 4 files changed, 17 insertions(+), 23 deletions(-) diff --git a/chanGroup_test.go b/chanGroup_test.go index 983436b..0e7cdea 100644 --- a/chanGroup_test.go +++ b/chanGroup_test.go @@ -5,6 +5,7 @@ import ( "database/sql/driver" "sync" + "github.com/infobloxopen/hotload/logger" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) @@ -51,7 +52,7 @@ var _ = Describe("Driver", func() { sqlDriver: nil, mu: sync.RWMutex{}, conns: conns, - log: DefaultLogger{}, + log: logger.DefaultLogger, } }) diff --git a/driver.go b/driver.go index cd92160..9d89179 100644 --- a/driver.go +++ b/driver.go @@ -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. @@ -206,7 +200,7 @@ 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 { @@ -214,7 +208,7 @@ func (cg *chanGroup) run() { continue } cg.valueChanged(v) - cg.log.Debug("connection information changed") + cg.log("connection information changed") } } } @@ -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") } } @@ -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 } diff --git a/fsnotify/filewatcher.go b/fsnotify/filewatcher.go index 5b8b787..59b4885 100644 --- a/fsnotify/filewatcher.go +++ b/fsnotify/filewatcher.go @@ -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 @@ -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 } @@ -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 @@ -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 @@ -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 } diff --git a/logger/logger.go b/logger/logger.go index 089437e..a73d6d3 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -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{}) {}