Skip to content

Commit

Permalink
Error and log handling (#23)
Browse files Browse the repository at this point in the history
Signed-off-by: Sotirios Mantziaris <[email protected]>
  • Loading branch information
mantzas authored and c0nstantx committed Jun 26, 2019
1 parent 771f3f4 commit 4b07d26
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 23 deletions.
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func (f *Field) Set(value string, version uint64) error {
return fmt.Errorf("the set call returned %d values: %v", len(rr), rr)
}
f.version = version
log.Infof("field %s updated with value %s, version: %d", f.name, value, version)
return nil
}

Expand Down
10 changes: 5 additions & 5 deletions harvester.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ func New(cfg interface{}) *Builder {
}

// WithConsulSeed enables support for seeding values with consul.
func (b *Builder) WithConsulSeed(addr, datacenter, token string, timeout time.Duration) *Builder {
func (b *Builder) WithConsulSeed(addr, dataCenter, token string, timeout time.Duration) *Builder {
if b.err != nil {
return b
}
getter, err := seedConsul.New(addr, datacenter, token, timeout)
getter, err := seedConsul.New(addr, dataCenter, token, timeout)
if err != nil {
b.err = err
return b
Expand Down Expand Up @@ -103,15 +103,15 @@ func (b *Builder) Create() (Harvester, error) {
if b.err != nil {
return nil, b.err
}
seed := seed.New(b.seedParams...)
sd := seed.New(b.seedParams...)

var mon Monitor
if len(b.watchers) == 0 {
return &harvester{seeder: seed, cfg: b.cfg}, nil
return &harvester{seeder: sd, cfg: b.cfg}, nil
}
mon, err := monitor.New(b.cfg, b.watchers...)
if err != nil {
return nil, err
}
return &harvester{seeder: seed, monitor: mon, cfg: b.cfg}, nil
return &harvester{seeder: sd, monitor: mon, cfg: b.cfg}, nil
}
14 changes: 13 additions & 1 deletion log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package log

import (
"errors"
"io"
"log"
"os"
)

// Func function definition.
Expand All @@ -18,10 +20,11 @@ var (
errorf = func(format string, v ...interface{}) {
log.Printf("ERROR: "+format, v...)
}
writer io.Writer = os.Stdout
)

// Setup allows for setting up custom loggers.
func Setup(inf, waf, erf Func) error {
func Setup(wr io.Writer, inf, waf, erf Func) error {
if inf == nil {
return errors.New("info log function is nil")
}
Expand All @@ -31,12 +34,21 @@ func Setup(inf, waf, erf Func) error {
if erf == nil {
return errors.New("error log function is nil")
}
if wr == nil {
return errors.New("writer is nil")
}
infof = inf
warnf = waf
errorf = erf
writer = wr
return nil
}

// Writer returns the loggers writer interface.
func Writer() io.Writer {
return writer
}

// Infof provides log info capabilities.
func Infof(format string, v ...interface{}) {
infof(format, v...)
Expand Down
15 changes: 10 additions & 5 deletions log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package log

import (
"bytes"
"io"
"log"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -19,11 +21,13 @@ func TestLog(t *testing.T) {
buf.Reset()
Errorf("Test %s", "logging")
assert.Contains(t, buf.String(), "ERROR: Test logging")
assert.Equal(t, os.Stdout, Writer())
}

func TestSetupLogging(t *testing.T) {
stubLogf := func(string, ...interface{}) {}
type args struct {
writer io.Writer
infof Func
warnf Func
errorf Func
Expand All @@ -33,14 +37,15 @@ func TestSetupLogging(t *testing.T) {
args args
wantErr bool
}{
{name: "success", args: args{infof: stubLogf, warnf: stubLogf, errorf: stubLogf}, wantErr: false},
{name: "missing info", args: args{infof: nil, warnf: stubLogf, errorf: stubLogf}, wantErr: true},
{name: "missing warn", args: args{infof: stubLogf, warnf: nil, errorf: stubLogf}, wantErr: true},
{name: "missing error", args: args{infof: stubLogf, warnf: stubLogf, errorf: nil}, wantErr: true},
{name: "success", args: args{writer: os.Stdin, infof: stubLogf, warnf: stubLogf, errorf: stubLogf}, wantErr: false},
{name: "missing writer", args: args{writer: nil, infof: stubLogf, warnf: stubLogf, errorf: stubLogf}, wantErr: true},
{name: "missing info", args: args{writer: os.Stdin, infof: nil, warnf: stubLogf, errorf: stubLogf}, wantErr: true},
{name: "missing warn", args: args{writer: os.Stdin, infof: stubLogf, warnf: nil, errorf: stubLogf}, wantErr: true},
{name: "missing error", args: args{writer: os.Stdin, infof: stubLogf, warnf: stubLogf, errorf: nil}, wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := Setup(tt.args.infof, tt.args.warnf, tt.args.errorf)
err := Setup(tt.args.writer, tt.args.infof, tt.args.warnf, tt.args.errorf)
if tt.wantErr {
assert.Error(t, err)
} else {
Expand Down
23 changes: 11 additions & 12 deletions monitor/consul/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"log"
"os"
"time"

"github.com/beatlabs/harvester/change"
Expand Down Expand Up @@ -79,24 +78,21 @@ func (w *Watcher) Watch(ctx context.Context, ch chan<- []*change.Change) error {
var err error
switch i.tp {
case "key":
pl, err = w.runKeyWatcher(i.key, ch)
pl, err = w.createKeyPlan(i.key, ch)
case "keyprefix":
pl, err = w.runPrefixWatcher(i.key, ch)
pl, err = w.createKeyPrefixPlan(i.key, ch)
}
if err != nil {
return err
}
w.pp = append(w.pp, pl)
go func(tp, key string) {
//TODO: Logger...
output := pl.LogOutput
if output == nil {
output = os.Stderr
}
logger := log.New(output, "", log.LstdFlags)
logger := log.New(harvesterlog.Writer(), "", 0)
err := pl.RunWithClientAndLogger(w.cl, logger)
if err != nil {
harvesterlog.Errorf("plan %s of type %s failed: %v", tp, key, err)
} else {
harvesterlog.Infof("plan %s of type %s is running", tp, key)
}
}(i.tp, i.key)
}
Expand All @@ -105,12 +101,13 @@ func (w *Watcher) Watch(ctx context.Context, ch chan<- []*change.Change) error {
for _, pl := range w.pp {
pl.Stop()
}
harvesterlog.Infof("all watch plans have been stopped")
}()

return nil
}

func (w *Watcher) runKeyWatcher(key string, ch chan<- []*change.Change) (*watch.Plan, error) {
func (w *Watcher) createKeyPlan(key string, ch chan<- []*change.Change) (*watch.Plan, error) {
pl, err := w.getPlan("key", key)
if err != nil {
return nil, err
Expand All @@ -123,11 +120,12 @@ func (w *Watcher) runKeyWatcher(key string, ch chan<- []*change.Change) (*watch.
ch <- []*change.Change{change.New(config.SourceConsul, pair.Key, string(pair.Value), pair.ModifyIndex)}
}
}
harvesterlog.Infof("plan for key %s created", key)
return pl, nil
}

func (w *Watcher) runPrefixWatcher(key string, ch chan<- []*change.Change) (*watch.Plan, error) {
pl, err := w.getPlan("keyprefix", key)
func (w *Watcher) createKeyPrefixPlan(keyPrefix string, ch chan<- []*change.Change) (*watch.Plan, error) {
pl, err := w.getPlan("keyprefix", keyPrefix)
if err != nil {
return nil, err
}
Expand All @@ -143,6 +141,7 @@ func (w *Watcher) runPrefixWatcher(key string, ch chan<- []*change.Change) (*wat
ch <- cc
}
}
harvesterlog.Infof("plan for keyprefix %s created", keyPrefix)
return pl, nil
}

Expand Down

0 comments on commit 4b07d26

Please sign in to comment.