diff --git a/pkg/fileutil/purge.go b/pkg/fileutil/purge.go index 92fceab01..fda96c371 100644 --- a/pkg/fileutil/purge.go +++ b/pkg/fileutil/purge.go @@ -20,14 +20,16 @@ import ( "sort" "strings" "time" + + "go.uber.org/zap" ) -func PurgeFile(dirname string, suffix string, max uint, interval time.Duration, stop <-chan struct{}) <-chan error { - return purgeFile(dirname, suffix, max, interval, stop, nil) +func PurgeFile(lg *zap.Logger, dirname string, suffix string, max uint, interval time.Duration, stop <-chan struct{}) <-chan error { + return purgeFile(lg, dirname, suffix, max, interval, stop, nil) } // purgeFile is the internal implementation for PurgeFile which can post purged files to purgec if non-nil. -func purgeFile(dirname string, suffix string, max uint, interval time.Duration, stop <-chan struct{}, purgec chan<- string) <-chan error { +func purgeFile(lg *zap.Logger, dirname string, suffix string, max uint, interval time.Duration, stop <-chan struct{}, purgec chan<- string) <-chan error { errC := make(chan error, 1) go func() { for { @@ -55,11 +57,19 @@ func purgeFile(dirname string, suffix string, max uint, interval time.Duration, return } if err = l.Close(); err != nil { - plog.Errorf("error unlocking %s when purging file (%v)", l.Name(), err) + if lg != nil { + lg.Warn("failed to unlock/close", zap.String("path", l.Name()), zap.Error(err)) + } else { + plog.Errorf("error unlocking %s when purging file (%v)", l.Name(), err) + } errC <- err return } - plog.Infof("purged file %s successfully", f) + if lg != nil { + lg.Info("purged", zap.String("path", f)) + } else { + plog.Infof("purged file %s successfully", f) + } newfnames = newfnames[1:] } if purgec != nil { diff --git a/pkg/fileutil/purge_test.go b/pkg/fileutil/purge_test.go index addd8e82e..fe313e50f 100644 --- a/pkg/fileutil/purge_test.go +++ b/pkg/fileutil/purge_test.go @@ -22,6 +22,8 @@ import ( "reflect" "testing" "time" + + "go.uber.org/zap" ) func TestPurgeFile(t *testing.T) { @@ -43,7 +45,7 @@ func TestPurgeFile(t *testing.T) { stop, purgec := make(chan struct{}), make(chan string, 10) // keep 3 most recent files - errch := purgeFile(dir, "test", 3, time.Millisecond, stop, purgec) + errch := purgeFile(zap.NewExample(), dir, "test", 3, time.Millisecond, stop, purgec) select { case f := <-purgec: t.Errorf("unexpected purge on %q", f) @@ -114,7 +116,7 @@ func TestPurgeFileHoldingLockFile(t *testing.T) { } stop, purgec := make(chan struct{}), make(chan string, 10) - errch := purgeFile(dir, "test", 3, time.Millisecond, stop, purgec) + errch := purgeFile(zap.NewExample(), dir, "test", 3, time.Millisecond, stop, purgec) for i := 0; i < 5; i++ { select { diff --git a/pkg/osutil/interrupt_unix.go b/pkg/osutil/interrupt_unix.go index b9feaffc9..315b2c66d 100644 --- a/pkg/osutil/interrupt_unix.go +++ b/pkg/osutil/interrupt_unix.go @@ -21,6 +21,8 @@ import ( "os/signal" "sync" "syscall" + + "go.uber.org/zap" ) // InterruptHandler is a function that is called on receiving a @@ -43,7 +45,7 @@ func RegisterInterruptHandler(h InterruptHandler) { } // HandleInterrupts calls the handler functions on receiving a SIGINT or SIGTERM. -func HandleInterrupts() { +func HandleInterrupts(lg *zap.Logger) { notifier := make(chan os.Signal, 1) signal.Notify(notifier, syscall.SIGINT, syscall.SIGTERM) @@ -57,7 +59,11 @@ func HandleInterrupts() { interruptExitMu.Lock() - plog.Noticef("received %v signal, shutting down...", sig) + if lg != nil { + lg.Info("received signal; shutting down", zap.String("signal", sig.String())) + } else { + plog.Noticef("received %v signal, shutting down...", sig) + } for _, h := range ihs { h() diff --git a/pkg/osutil/interrupt_windows.go b/pkg/osutil/interrupt_windows.go index 013ae88e1..ec67ba5d2 100644 --- a/pkg/osutil/interrupt_windows.go +++ b/pkg/osutil/interrupt_windows.go @@ -16,7 +16,11 @@ package osutil -import "os" +import ( + "os" + + "go.uber.org/zap" +) type InterruptHandler func() @@ -24,7 +28,7 @@ type InterruptHandler func() func RegisterInterruptHandler(h InterruptHandler) {} // HandleInterrupts is a no-op on windows -func HandleInterrupts() {} +func HandleInterrupts(*zap.Logger) {} // Exit calls os.Exit func Exit(code int) { diff --git a/pkg/osutil/osutil_test.go b/pkg/osutil/osutil_test.go index 9fbc7a44c..c03895b8a 100644 --- a/pkg/osutil/osutil_test.go +++ b/pkg/osutil/osutil_test.go @@ -21,6 +21,8 @@ import ( "syscall" "testing" "time" + + "go.uber.org/zap" ) func init() { setDflSignal = func(syscall.Signal) {} } @@ -69,7 +71,7 @@ func TestHandleInterrupts(t *testing.T) { c := make(chan os.Signal, 2) signal.Notify(c, sig) - HandleInterrupts() + HandleInterrupts(zap.NewExample()) syscall.Kill(syscall.Getpid(), sig) // we should receive the signal once from our own kill and