From f34aaf7ef17aa10c7ba1923d0694347e47634192 Mon Sep 17 00:00:00 2001 From: Etienne Perot Date: Wed, 13 Jan 2021 15:07:24 -0800 Subject: testutil: Create a `multiLogger` that logs to multiple `Loggers`. This is useful when using the shell library in order to log to both the test log and the standard logs. PiperOrigin-RevId: 351673465 --- pkg/test/testutil/testutil.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pkg/test/testutil/testutil.go b/pkg/test/testutil/testutil.go index fdd416b5e..2b2f95c13 100644 --- a/pkg/test/testutil/testutil.go +++ b/pkg/test/testutil/testutil.go @@ -111,6 +111,30 @@ func (d DefaultLogger) Logf(fmt string, args ...interface{}) { log.Printf(fmt, args...) } +// multiLogger logs to multiple Loggers. +type multiLogger []Logger + +// Name implements Logger.Name. +func (m multiLogger) Name() string { + names := make([]string, len(m)) + for i, l := range m { + names[i] = l.Name() + } + return strings.Join(names, "+") +} + +// Logf implements Logger.Logf. +func (m multiLogger) Logf(fmt string, args ...interface{}) { + for _, l := range m { + l.Logf(fmt, args...) + } +} + +// NewMultiLogger returns a new Logger that logs on multiple Loggers. +func NewMultiLogger(loggers ...Logger) Logger { + return multiLogger(loggers) +} + // Cmd is a simple wrapper. type Cmd struct { logger Logger -- cgit v1.2.3