diff --git a/log/log.go b/log/log.go index a4b076ee..a29aee89 100644 --- a/log/log.go +++ b/log/log.go @@ -11,6 +11,9 @@ import ( type Func func(string, ...interface{}) var ( + debugf = func(format string, v ...interface{}) { + log.Printf("DEBUG: "+format, v...) + } infof = func(format string, v ...interface{}) { log.Printf("INFO: "+format, v...) } @@ -24,7 +27,7 @@ var ( ) // Setup allows for setting up custom loggers. -func Setup(wr io.Writer, inf, waf, erf Func) error { +func Setup(wr io.Writer, inf, waf, erf, dbf Func) error { if inf == nil { return errors.New("info log function is nil") } @@ -34,12 +37,16 @@ func Setup(wr io.Writer, inf, waf, erf Func) error { if erf == nil { return errors.New("error log function is nil") } + if dbf == nil { + return errors.New("debug log function is nil") + } if wr == nil { return errors.New("writer is nil") } infof = inf warnf = waf errorf = erf + debugf = dbf writer = wr return nil } @@ -63,3 +70,8 @@ func Warnf(format string, v ...interface{}) { func Errorf(format string, v ...interface{}) { errorf(format, v...) } + +// Debugf provides log debug capabilities. +func Debugf(format string, v ...interface{}) { + debugf(format, v...) +} diff --git a/log/log_test.go b/log/log_test.go index 50b8d8d7..2e6e848f 100644 --- a/log/log_test.go +++ b/log/log_test.go @@ -19,6 +19,9 @@ func TestLog(t *testing.T) { Warnf("Test %s", "logging") assert.Contains(t, buf.String(), "WARN: Test logging") buf.Reset() + Debugf("Test %s", "logging") + assert.Contains(t, buf.String(), "DEBUG: Test logging") + buf.Reset() Errorf("Test %s", "logging") assert.Contains(t, buf.String(), "ERROR: Test logging") assert.Equal(t, os.Stdout, Writer()) @@ -31,21 +34,23 @@ func TestSetupLogging(t *testing.T) { infof Func warnf Func errorf Func + debugf Func } tests := []struct { name string args args wantErr bool }{ - {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}, + {name: "success", args: args{writer: os.Stdin, infof: stubLogf, warnf: stubLogf, errorf: stubLogf, debugf: stubLogf}, wantErr: false}, + {name: "missing writer", args: args{writer: nil, infof: stubLogf, warnf: stubLogf, errorf: stubLogf, debugf: stubLogf}, wantErr: true}, + {name: "missing info", args: args{writer: os.Stdin, infof: nil, warnf: stubLogf, errorf: stubLogf, debugf: stubLogf}, wantErr: true}, + {name: "missing warn", args: args{writer: os.Stdin, infof: stubLogf, warnf: nil, errorf: stubLogf, debugf: stubLogf}, wantErr: true}, + {name: "missing error", args: args{writer: os.Stdin, infof: stubLogf, warnf: stubLogf, errorf: nil, debugf: stubLogf}, wantErr: true}, + {name: "missing debug", args: args{writer: os.Stdin, infof: stubLogf, warnf: stubLogf, errorf: stubLogf}, wantErr: true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - err := Setup(tt.args.writer, tt.args.infof, tt.args.warnf, tt.args.errorf) + err := Setup(tt.args.writer, tt.args.infof, tt.args.warnf, tt.args.errorf, tt.args.debugf) if tt.wantErr { assert.Error(t, err) } else { diff --git a/monitor/consul/watcher.go b/monitor/consul/watcher.go index bcc03918..07a805f2 100644 --- a/monitor/consul/watcher.go +++ b/monitor/consul/watcher.go @@ -92,7 +92,7 @@ func (w *Watcher) Watch(ctx context.Context, ch chan<- []*change.Change) error { 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) + harvesterlog.Debugf("plan %s of type %s is running", tp, key) } }(i.tp, i.key) }