Skip to content
Snippets Groups Projects
Commit 2f75d76afdfa authored by Christophe de Vienne's avatar Christophe de Vienne
Browse files

Add the 'testutils' package

parent d6032c71d396
No related branches found
No related tags found
No related merge requests found
Pipeline #6241 passed
package testutils
import (
"os"
"testing"
"github.com/rs/zerolog"
)
//NewTestLogger creates a TestLogger
func NewTestLogger(tb testing.TB) *TestLogger {
return &TestLogger{tb}
}
// TestLogger logs to a t.Log function
type TestLogger struct {
tb testing.TB
}
// Logger returns a zerolog Logger
func (tl *TestLogger) Logger() zerolog.Logger {
level := zerolog.DebugLevel
if os.Getenv("TEST_TRACE") != "" {
level = zerolog.TraceLevel
}
return zerolog.New(zerolog.ConsoleWriter{Out: tl}).
With().Timestamp().Logger().
Level(level)
}
// Write writes the given string to t.Logf
func (tl *TestLogger) Write(m []byte) (int, error) {
tl.tb.Logf(string(m))
return len(m), nil
}
// GetLogger returns a test Logger
func GetLogger(tb testing.TB) zerolog.Logger {
return NewTestLogger(tb).Logger()
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment