diff options
-rw-r--r-- | internal/testhelper/logger.go | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/internal/testhelper/logger.go b/internal/testhelper/logger.go index 63f165ae3..0b284924d 100644 --- a/internal/testhelper/logger.go +++ b/internal/testhelper/logger.go @@ -1,8 +1,8 @@ package testhelper import ( + "bytes" "fmt" - "io" "os" "path/filepath" "testing" @@ -12,6 +12,24 @@ import ( "gitlab.com/gitlab-org/gitaly/v16/internal/helper/perm" ) +// NewLogger returns a logger that records the log output and +// prints it out only if the test fails. +func NewLogger(tb testing.TB) *logrus.Logger { + logOutput := &bytes.Buffer{} + logger := logrus.New() //nolint:forbidigo + logger.Out = logOutput + + tb.Cleanup(func() { + if !tb.Failed() { + return + } + + tb.Logf("Recorded logs:\n%s\n", logOutput) + }) + + return logger +} + // NewDiscardingLogger creates a logger that discards everything. func NewDiscardingLogger(tb testing.TB) *logrus.Logger { logger := logrus.New() //nolint:forbidigo |