Skip to content

Commit

Permalink
Add support for Debugf (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmaz42 authored Jun 18, 2020
1 parent 440d83b commit 684d4c5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
14 changes: 13 additions & 1 deletion log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
Expand All @@ -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")
}
Expand All @@ -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
}
Expand All @@ -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...)
}
17 changes: 11 additions & 6 deletions log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion monitor/consul/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 684d4c5

Please sign in to comment.