pkg: support structured logger

Signed-off-by: Gyuho Lee <gyuhox@gmail.com>
This commit is contained in:
Gyuho Lee
2018-04-16 03:57:43 -07:00
parent bdbed26f64
commit 0dad8abb6f
5 changed files with 36 additions and 12 deletions

View File

@ -20,14 +20,16 @@ import (
"sort" "sort"
"strings" "strings"
"time" "time"
"go.uber.org/zap"
) )
func PurgeFile(dirname string, suffix string, max uint, interval time.Duration, stop <-chan struct{}) <-chan error { func PurgeFile(lg *zap.Logger, dirname string, suffix string, max uint, interval time.Duration, stop <-chan struct{}) <-chan error {
return purgeFile(dirname, suffix, max, interval, stop, nil) 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. // 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) errC := make(chan error, 1)
go func() { go func() {
for { for {
@ -55,11 +57,19 @@ func purgeFile(dirname string, suffix string, max uint, interval time.Duration,
return return
} }
if err = l.Close(); err != nil { if err = l.Close(); err != nil {
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) plog.Errorf("error unlocking %s when purging file (%v)", l.Name(), err)
}
errC <- err errC <- err
return return
} }
if lg != nil {
lg.Info("purged", zap.String("path", f))
} else {
plog.Infof("purged file %s successfully", f) plog.Infof("purged file %s successfully", f)
}
newfnames = newfnames[1:] newfnames = newfnames[1:]
} }
if purgec != nil { if purgec != nil {

View File

@ -22,6 +22,8 @@ import (
"reflect" "reflect"
"testing" "testing"
"time" "time"
"go.uber.org/zap"
) )
func TestPurgeFile(t *testing.T) { func TestPurgeFile(t *testing.T) {
@ -43,7 +45,7 @@ func TestPurgeFile(t *testing.T) {
stop, purgec := make(chan struct{}), make(chan string, 10) stop, purgec := make(chan struct{}), make(chan string, 10)
// keep 3 most recent files // 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 { select {
case f := <-purgec: case f := <-purgec:
t.Errorf("unexpected purge on %q", f) 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) 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++ { for i := 0; i < 5; i++ {
select { select {

View File

@ -21,6 +21,8 @@ import (
"os/signal" "os/signal"
"sync" "sync"
"syscall" "syscall"
"go.uber.org/zap"
) )
// InterruptHandler is a function that is called on receiving a // 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. // HandleInterrupts calls the handler functions on receiving a SIGINT or SIGTERM.
func HandleInterrupts() { func HandleInterrupts(lg *zap.Logger) {
notifier := make(chan os.Signal, 1) notifier := make(chan os.Signal, 1)
signal.Notify(notifier, syscall.SIGINT, syscall.SIGTERM) signal.Notify(notifier, syscall.SIGINT, syscall.SIGTERM)
@ -57,7 +59,11 @@ func HandleInterrupts() {
interruptExitMu.Lock() interruptExitMu.Lock()
if lg != nil {
lg.Info("received signal; shutting down", zap.String("signal", sig.String()))
} else {
plog.Noticef("received %v signal, shutting down...", sig) plog.Noticef("received %v signal, shutting down...", sig)
}
for _, h := range ihs { for _, h := range ihs {
h() h()

View File

@ -16,7 +16,11 @@
package osutil package osutil
import "os" import (
"os"
"go.uber.org/zap"
)
type InterruptHandler func() type InterruptHandler func()
@ -24,7 +28,7 @@ type InterruptHandler func()
func RegisterInterruptHandler(h InterruptHandler) {} func RegisterInterruptHandler(h InterruptHandler) {}
// HandleInterrupts is a no-op on windows // HandleInterrupts is a no-op on windows
func HandleInterrupts() {} func HandleInterrupts(*zap.Logger) {}
// Exit calls os.Exit // Exit calls os.Exit
func Exit(code int) { func Exit(code int) {

View File

@ -21,6 +21,8 @@ import (
"syscall" "syscall"
"testing" "testing"
"time" "time"
"go.uber.org/zap"
) )
func init() { setDflSignal = func(syscall.Signal) {} } func init() { setDflSignal = func(syscall.Signal) {} }
@ -69,7 +71,7 @@ func TestHandleInterrupts(t *testing.T) {
c := make(chan os.Signal, 2) c := make(chan os.Signal, 2)
signal.Notify(c, sig) signal.Notify(c, sig)
HandleInterrupts() HandleInterrupts(zap.NewExample())
syscall.Kill(syscall.Getpid(), sig) syscall.Kill(syscall.Getpid(), sig)
// we should receive the signal once from our own kill and // we should receive the signal once from our own kill and