diff options
author | Sami Hiltunen <shiltunen@gitlab.com> | 2023-08-22 16:19:23 +0300 |
---|---|---|
committer | Sami Hiltunen <shiltunen@gitlab.com> | 2023-08-31 09:28:30 +0300 |
commit | c5d9857e0080960010d5df7343b4faf009556f6c (patch) | |
tree | a86e4d2878b9dc675e9d04efa76b051685631793 | |
parent | efe1a5d80ba3e4c80522fa731b575fa84ca908e4 (diff) |
Identify loggers with name
If test cases configure multiple loggers, each of their recorded logs
are printed out on failure. Take a name for the logger so the printed
logs can be identified simply by looking at the name.
-rw-r--r-- | internal/testhelper/logger.go | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/internal/testhelper/logger.go b/internal/testhelper/logger.go index c97d2bff0..83a94b129 100644 --- a/internal/testhelper/logger.go +++ b/internal/testhelper/logger.go @@ -13,19 +13,43 @@ import ( "gitlab.com/gitlab-org/gitaly/v16/internal/helper/perm" ) +type loggerOptions struct { + name string +} + +// LoggerOption configures a logger. +type LoggerOption func(*loggerOptions) + +// WithLoggerName sets the name of the logger. The name is included along +// the logs to help identifying the logs if multiple loggers are used. +func WithLoggerName(name string) LoggerOption { + return func(opts *loggerOptions) { + opts.name = name + } +} + // 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 { +func NewLogger(tb testing.TB, options ...LoggerOption) *logrus.Logger { logOutput := &bytes.Buffer{} logger := logrus.New() //nolint:forbidigo logger.Out = logOutput + var opts loggerOptions + for _, apply := range options { + apply(&opts) + } + tb.Cleanup(func() { if !tb.Failed() { return } - tb.Logf("Recorded logs:\n%s\n", logOutput) + if opts.name != "" { + tb.Logf("Recorded logs of %q:\n%s\n", opts.name, logOutput) + } else { + tb.Logf("Recorded test logs:\n%s\n", logOutput) + } }) return logger |