From c2f8c05bb5fba3123c419683feef70a3c2a458c4 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Sun, 17 Jan 2016 16:42:05 +0300 Subject: [PATCH] Add minimalistic Logger interface --- daemon.go | 15 +++++++++------ satan.go | 8 +++++++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/daemon.go b/daemon.go index 48ad5bf..2fb5963 100644 --- a/daemon.go +++ b/daemon.go @@ -3,7 +3,6 @@ package satan import ( "errors" "fmt" - "log" "strings" "time" @@ -48,7 +47,7 @@ type BaseDaemon struct { self Daemon name string queue chan<- *task - logger *log.Logger + logger Logger panicHandler PanicHandler subscriber Subscriber publisher Publisher @@ -128,10 +127,10 @@ func (d *BaseDaemon) Publish(msg []byte) { func (d *BaseDaemon) LimitRate(times int, per time.Duration) { rate := float64(time.Second) / float64(per) * float64(times) if rate <= 0 { - d.logger.Println("Daemon %s processing rate was limited to %d. Using 1 instead", d.base(), rate) + d.Logf("Daemon %s processing rate was limited to %d. Using 1 instead", d.base(), rate) rate = 1.0 } - d.logger.Printf("Daemon %s processing rate is limited to %.2f ops/s", d.base(), rate) + d.Logf("Daemon %s processing rate is limited to %.2f ops/s", d.base(), rate) d.limit = ratelimit.NewBucketWithRate(rate, 1) } @@ -157,11 +156,15 @@ func (d *BaseDaemon) Continue() bool { } func (d *BaseDaemon) Log(v ...interface{}) { - d.logger.Println(v...) + if d.logger != nil { + d.logger.Println(v...) + } } func (d *BaseDaemon) Logf(format string, v ...interface{}) { - d.logger.Printf(format, v...) + if d.logger != nil { + d.logger.Printf(format, v...) + } } func (d *BaseDaemon) Shutdown() {} diff --git a/satan.go b/satan.go index 78ac167..4b3e015 100644 --- a/satan.go +++ b/satan.go @@ -16,7 +16,7 @@ type Satan struct { Subscriber Subscriber Publisher Publisher DaemonStats stats.Publisher - Logger *log.Logger + Logger Logger NumWorkers int daemons []Daemon @@ -51,6 +51,12 @@ type Publisher interface { Close() } +// Logger is the interface that implements minimal logging functions. +type Logger interface { + Printf(format string, v ...interface{}) + Println(v ...interface{}) +} + type task struct { daemon Daemon actor Actor