diff --git a/filewatcher/filewatcher.go b/filewatcher/filewatcher.go index 6904b9c..82d24cf 100644 --- a/filewatcher/filewatcher.go +++ b/filewatcher/filewatcher.go @@ -27,13 +27,15 @@ import ( ) type FileWatcher struct { + logger *logp.Logger files []string lastScan time.Time lastHash uint64 } -func New(files ...string) *FileWatcher { +func New(logger *logp.Logger, files ...string) *FileWatcher { return &FileWatcher{ + logger: logger, lastScan: time.Time{}, lastHash: 0, files: files, @@ -57,7 +59,7 @@ func (f *FileWatcher) Scan() ([]string, bool, error) { for _, path := range f.files { info, err := os.Stat(path) if err != nil { - logp.Err("Error getting stats for file: %s", path) + f.logger.Errorf("Error getting stats for file: %s", path) continue } diff --git a/filewatcher/filewatcher_test.go b/filewatcher/filewatcher_test.go index e657dfe..6506a2a 100644 --- a/filewatcher/filewatcher_test.go +++ b/filewatcher/filewatcher_test.go @@ -23,6 +23,7 @@ import ( "testing" "time" + "github.com/elastic/elastic-agent-libs/logp/logptest" "github.com/stretchr/testify/assert" ) @@ -44,7 +45,7 @@ func TestFileWatcher(t *testing.T) { filenames = append(filenames, filename) } - watcher := New(filenames...) + watcher := New(logptest.NewTestingLogger(t, ""), filenames...) // Modification timestamps usually have second precision, // we wait to make sure we're not in the second the files were created diff --git a/service/service.go b/service/service.go index 538f3fd..b278de1 100644 --- a/service/service.go +++ b/service/service.go @@ -55,7 +55,7 @@ func HandleSignals(stopFunction func(), cancel context.CancelFunc) { }() // Handle the Windows service events - go ProcessWindowsControlEvents(func() { + go ProcessWindowsControlEvents(logger, func() { logger.Info("Received Windows SVC stop/shutdown request") callback.Do(stopFunction) }) diff --git a/service/service_unix.go b/service/service_unix.go index 38784f0..dab85a0 100644 --- a/service/service_unix.go +++ b/service/service_unix.go @@ -19,8 +19,10 @@ package service +import "github.com/elastic/elastic-agent-libs/logp" + // ProcessWindowsControlEvents is not used on non-windows platforms. -func ProcessWindowsControlEvents(stopCallback func()) { +func ProcessWindowsControlEvents(logger *logp.Logger, stopCallback func()) { } func notifyWindowsServiceStopped() { diff --git a/service/service_windows.go b/service/service_windows.go index 77f385e..ff9a6b7 100644 --- a/service/service_windows.go +++ b/service/service_windows.go @@ -122,16 +122,16 @@ const couldNotConnect syscall.Errno = 1063 // On non-windows platforms, the function does nothing. The // stopCallback function is called when the Stop/Shutdown // request is received. -func ProcessWindowsControlEvents(stopCallback func()) { +func ProcessWindowsControlEvents(logger *logp.Logger, stopCallback func()) { defer close(serviceInstance.executeFinished) //nolint:staticcheck // keep using the deprecated method in order to maintain the existing behavior isInteractive, err := svc.IsAnInteractiveSession() if err != nil { - logp.Err("IsAnInteractiveSession: %v", err) + logger.Errorf("IsAnInteractiveSession: %v", err) return } - logp.Debug("service", "Windows is interactive: %v", isInteractive) + logger.Named("service")..Debugf("Windows is interactive: %v", isInteractive) run := svc.Run if isInteractive { @@ -160,11 +160,11 @@ func ProcessWindowsControlEvents(stopCallback func()) { If the program will be run as a console application for debugging purposes, structure it such that service-specific code is not called when this error is returned." */ - logp.Info("Attempted to register Windows service handlers, but this is not a service. No action necessary") + logger.Info("Attempted to register Windows service handlers, but this is not a service. No action necessary") return } - logp.Err("Windows service setup failed: %+v", err) + logger.Errorf("Windows service setup failed: %+v", err) } // WaitExecutionDone returns only after stop was reported to service manager.