chore: change implementation to be able to handle WithFields etc.

This commit is contained in:
2021-01-27 09:07:03 +01:00
parent 8bf207511a
commit 4e2a280ad1
2 changed files with 22 additions and 466 deletions
+18 -79
View File
@@ -24,14 +24,30 @@ import (
"github.com/apex/log"
"github.com/sanity-io/litter"
"testing"
"time"
)
// Mock implements `log.Interface` for use in unit-testing
// Mock has a Logger for use in unit-testing
type Mock struct {
Logger log.Interface
Logged []string
}
// HandleLog stores the logged entries to be able to check them later
func (m *Mock) HandleLog(entry *log.Entry) error {
m.Logged = append(m.Logged, fmt.Sprintf("%s: %s", entry.Level.String(), entry.Message))
return nil
}
var _ log.Handler = &Mock{}
func New() *Mock {
mock := &Mock{
Logger: log.Log,
}
log.SetHandler(mock)
return mock
}
// Check verifies that the application has logged the expected strings
func (m *Mock) Check(t testing.TB, wantLogged []string) {
t.Helper()
@@ -43,80 +59,3 @@ func (m *Mock) Check(t testing.TB, wantLogged []string) {
}
}
}
// Debug implements log.Interface Debug
func (m *Mock) Debug(s string) {
m.Logged = append(m.Logged, fmt.Sprintf("DEBUG: %s", s))
}
// Info implements log.Interface Info
func (m *Mock) Info(s string) {
m.Logged = append(m.Logged, fmt.Sprintf("INFO: %s", s))
}
// Warn implements log.Interface Warn
func (m *Mock) Warn(s string) {
m.Logged = append(m.Logged, fmt.Sprintf("WARN: %s", s))
}
// Error implements log.Interface Error
func (m *Mock) Error(s string) {
m.Logged = append(m.Logged, fmt.Sprintf("ERROR: %s", s))
}
// Fatal implements log.Interface Fatal
func (m *Mock) Fatal(s string) {
m.Logged = append(m.Logged, fmt.Sprintf("FATAL: %s", s))
}
// Debugf implements log.Interface Debugf
func (m *Mock) Debugf(s string, i ...interface{}) {
m.Logged = append(m.Logged, fmt.Sprintf("DEBUG: %s", fmt.Sprintf(s, i...)))
}
// Infof implements log.Interface Infof
func (m *Mock) Infof(s string, i ...interface{}) {
m.Logged = append(m.Logged, fmt.Sprintf("INFO: %s", fmt.Sprintf(s, i...)))
}
// Warnf implements log.Interface Warnf
func (m *Mock) Warnf(s string, i ...interface{}) {
m.Logged = append(m.Logged, fmt.Sprintf("WARN: %s", fmt.Sprintf(s, i...)))
}
// Errorf implements log.Interface Errorf
func (m *Mock) Errorf(s string, i ...interface{}) {
m.Logged = append(m.Logged, fmt.Sprintf("ERROR: %s", fmt.Sprintf(s, i...)))
}
// Fatalf implements log.Interface Fatalf
func (m *Mock) Fatalf(s string, i ...interface{}) {
m.Logged = append(m.Logged, fmt.Sprintf("FATAL: %s", fmt.Sprintf(s, i...)))
}
// Trace implements log.Interface Trace
func (m *Mock) Trace(string) *log.Entry {
return nil
}
// WithFields implements log.Interface WithFields
func (m *Mock) WithFields(log.Fielder) *log.Entry {
return nil
}
// WithField implements log.Interface WithField
func (m *Mock) WithField(string, interface{}) *log.Entry {
return nil
}
// WithDuration implements log.Interface WithDuration
func (m *Mock) WithDuration(time.Duration) *log.Entry {
return nil
}
// WithError implements log.Interface WithError
func (m *Mock) WithError(error) *log.Entry {
return nil
}
var _ log.Interface = &Mock{}