summaryrefslogtreecommitdiffstats
path: root/pkg/fakes/log.go
blob: fc68f23c15ddb7d819a758d5f208671d0ba6971f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package fakes

import (
	"fmt"
	"testing"

	"github.com/sirupsen/logrus"
	"github.com/stretchr/testify/assert"
)

var _ logrus.FieldLogger = &FakeFieldLogger{}

// for now we're just tracking calls to the Error and Errorf methods
type FakeFieldLogger struct {
	loggedErrors []string
	*logrus.Entry
}

func (self *FakeFieldLogger) Error(args ...interface{}) {
	if len(args) != 1 {
		panic("Expected exactly one argument to FakeFieldLogger.Error")
	}

	switch arg := args[0].(type) {
	case error:
		self.loggedErrors = append(self.loggedErrors, arg.Error())
	case string:
		self.loggedErrors = append(self.loggedErrors, arg)
	}
}

func (self *FakeFieldLogger) Errorf(format string, args ...interface{}) {
	msg := fmt.Sprintf(format, args...)
	self.loggedErrors = append(self.loggedErrors, msg)
}

func (self *FakeFieldLogger) AssertErrors(t *testing.T, expectedErrors []string) {
	t.Helper()
	assert.EqualValues(t, expectedErrors, self.loggedErrors)
}