From 4f02f0e5bf25c88d08f6eadb37b4a859625fa34a Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Fri, 7 Mar 2025 10:51:14 -0600 Subject: [PATCH] watchers 2 --- core/http/httplogger.go | 2 +- core/logger/logger.go | 10 ++++----- core/watchers/watcher.go | 46 ++++++++++++++++++++++++++++------------ 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/core/http/httplogger.go b/core/http/httplogger.go index 0dee1e3..e86fa08 100644 --- a/core/http/httplogger.go +++ b/core/http/httplogger.go @@ -21,7 +21,7 @@ func LogRequest(log *logger.Logger, statusCode int, r *http.Request, duration ti statusColor := getStatusColor(statusCode) // Use the logger's raw message writer to bypass the standard format - log.LogRaw("%s [%s%d%s] %s %s (%v)", + log.LogRaw("%s [ %s%d%s] %s %s (%v)", time.Now().Format(log.TimeFormat()), statusColor, statusCode, colorReset, r.Method, r.URL.Path, duration) diff --git a/core/logger/logger.go b/core/logger/logger.go index ee0f9f2..0e75088 100644 --- a/core/logger/logger.go +++ b/core/logger/logger.go @@ -34,11 +34,11 @@ var levelProps = map[int]struct { tag string color string }{ - LevelDebug: {"DBG", colorCyan}, - LevelInfo: {"INF", colorBlue}, - LevelWarning: {"WRN", colorYellow}, - LevelError: {"ERR", colorRed}, - LevelFatal: {"FTL", colorPurple}, + LevelDebug: {" DBG", colorCyan}, + LevelInfo: {"INFO", colorBlue}, + LevelWarning: {"WARN", colorYellow}, + LevelError: {" ERR", colorRed}, + LevelFatal: {"FATL", colorPurple}, } // Time format for log messages diff --git a/core/watchers/watcher.go b/core/watchers/watcher.go index c984ce2..d758d14 100644 --- a/core/watchers/watcher.go +++ b/core/watchers/watcher.go @@ -1,6 +1,7 @@ package watchers import ( + "fmt" "os" "path/filepath" "sync" @@ -120,9 +121,9 @@ func WatchDirectory(config WatcherConfig) (*Watcher, error) { go w.debounceLoop() if config.Adaptive { - config.Log.Debug("Started watching directory with adaptive polling: %s", config.Dir) + w.logDebug("Started watching with adaptive polling") } else { - config.Log.Debug("Started watching directory: %s", config.Dir) + w.logDebug("Started watching with fixed polling interval: %v", pollInterval) } return w, nil @@ -147,7 +148,7 @@ func (w *Watcher) watchLoop() { case <-ticker.C: changed, err := w.checkForChanges() if err != nil { - w.log.Error("Error checking for changes: %v", err) + w.logError("Error checking for changes: %v", err) continue } @@ -155,11 +156,10 @@ func (w *Watcher) watchLoop() { // Update last change time w.lastChangeTime = time.Now() - // Reset to base polling interval if we were using extended intervals if w.adaptive && w.pollInterval > w.basePollInterval { w.pollInterval = w.basePollInterval ticker.Reset(w.pollInterval) - w.log.Debug("Reset to base polling interval: %v", w.pollInterval) + w.logDebug("Reset to base polling interval: %v", w.pollInterval) } // Try to send a change notification, non-blocking @@ -176,13 +176,13 @@ func (w *Watcher) watchLoop() { // First extension w.pollInterval = extendedPollInterval ticker.Reset(w.pollInterval) - w.log.Debug("Extended polling interval to %v after %v of inactivity", + w.logDebug("Extended polling interval to: %v after %v of inactivity", w.pollInterval, inactiveDuration.Round(time.Second)) } else if w.pollInterval == extendedPollInterval && inactiveDuration > secondExtendThreshold { // Second extension w.pollInterval = maxPollInterval ticker.Reset(w.pollInterval) - w.log.Debug("Extended polling interval to %v after %v of inactivity", + w.logDebug("Extended polling interval to: %v after %v of inactivity", w.pollInterval, inactiveDuration.Round(time.Second)) } } @@ -209,7 +209,7 @@ func (w *Watcher) debounceLoop() { // Start a new timer timer = time.AfterFunc(w.debounceTime, func() { if err := w.callback(); err != nil { - w.log.Error("Refresh callback error: %v", err) + w.logError("Refresh callback error: %v", err) } }) @@ -229,7 +229,7 @@ func (w *Watcher) scanDirectory() error { return filepath.Walk(w.dir, func(path string, info os.FileInfo, err error) error { if err != nil { - w.log.Warning("Error accessing path %s: %v", path, err) + w.logWarning("Error accessing path %s: %v", path, err) return nil // Continue with other files } @@ -248,7 +248,25 @@ func (w *Watcher) scanDirectory() error { }) } -// checkForChanges compares current file state with the last scan +// logDebug logs a debug message with the watcher's directory prefix +func (w *Watcher) logDebug(format string, args ...any) { + w.log.Debug("[Watcher] [%s] %s", w.dir, fmt.Sprintf(format, args...)) +} + +// logInfo logs an info message with the watcher's directory prefix +func (w *Watcher) logInfo(format string, args ...any) { + w.log.Info("[Watcher] [%s] %s", w.dir, fmt.Sprintf(format, args...)) +} + +// logWarning logs a warning message with the watcher's directory prefix +func (w *Watcher) logWarning(format string, args ...any) { + w.log.Warning("[Watcher] [%s] %s", w.dir, fmt.Sprintf(format, args...)) +} + +// logError logs an error message with the watcher's directory prefix +func (w *Watcher) logError(format string, args ...any) { + w.log.Error("[Watcher] [%s] %s", w.dir, fmt.Sprintf(format, args...)) +} func (w *Watcher) checkForChanges() (bool, error) { // Get current state currentFiles := make(map[string]FileInfo) @@ -284,7 +302,7 @@ func (w *Watcher) checkForChanges() (bool, error) { // Check for different file count (quick check) if len(currentFiles) != len(previousFiles) { - w.log.Debug("File count changed: %d -> %d", len(previousFiles), len(currentFiles)) + w.logDebug("File count changed: %d -> %d", len(previousFiles), len(currentFiles)) w.updateFiles(currentFiles) return true, nil } @@ -294,14 +312,14 @@ func (w *Watcher) checkForChanges() (bool, error) { prevInfo, exists := previousFiles[path] if !exists { // New file - w.log.Debug("New file detected: %s", path) + w.logDebug("New file detected: %s", path) w.updateFiles(currentFiles) return true, nil } if currentInfo.ModTime != prevInfo.ModTime || currentInfo.Size != prevInfo.Size { // File modified - w.log.Debug("File modified: %s", path) + w.logDebug("File modified: %s", path) w.updateFiles(currentFiles) return true, nil } @@ -311,7 +329,7 @@ func (w *Watcher) checkForChanges() (bool, error) { for path := range previousFiles { if _, exists := currentFiles[path]; !exists { // File deleted - w.log.Debug("File deleted: %s", path) + w.logDebug("File deleted: %s", path) w.updateFiles(currentFiles) return true, nil }