1
0
Fork 0

Add minimalistic Logger interface

This commit is contained in:
Gregory Eremin 2016-01-17 16:42:05 +03:00
parent 154c493b64
commit c2f8c05bb5
2 changed files with 16 additions and 7 deletions

View File

@ -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() {}

View File

@ -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