diff --git a/filebeat/input/filestream/config_test.go b/filebeat/input/filestream/config_test.go index 93b9c1d5173d..153d67ee1b42 100644 --- a/filebeat/input/filestream/config_test.go +++ b/filebeat/input/filestream/config_test.go @@ -28,7 +28,7 @@ import ( loginp "github.com/elastic/beats/v7/filebeat/input/filestream/internal/input-logfile" conf "github.com/elastic/elastic-agent-libs/config" - "github.com/elastic/elastic-agent-libs/logp" + "github.com/elastic/elastic-agent-libs/logp/logptest" ) func TestConfigValidate(t *testing.T) { @@ -229,13 +229,12 @@ id: unique-id-3 require.NoError(t, err, "could not create input configuration") inputs = append(inputs, cfg) } - err := logp.DevelopmentSetup(logp.ToObserverOutput()) - require.NoError(t, err, "could not setup log for development") - err = ValidateInputIDs(inputs, logp.L()) + logger, observedLogs := logptest.NewTestingLoggerWithObserver(t, "") + err := ValidateInputIDs(inputs, logger) tc.assertErr(t, err) if tc.assertLogs != nil { - tc.assertLogs(t, logp.ObserverLogs()) + tc.assertLogs(t, observedLogs) } }) } diff --git a/filebeat/input/journald/pkg/journalctl/reader_test.go b/filebeat/input/journald/pkg/journalctl/reader_test.go index ecbdea70112e..7a14972d72b5 100644 --- a/filebeat/input/journald/pkg/journalctl/reader_test.go +++ b/filebeat/input/journald/pkg/journalctl/reader_test.go @@ -31,6 +31,7 @@ import ( "github.com/elastic/beats/v7/filebeat/input/journald/pkg/journalfield" input "github.com/elastic/beats/v7/filebeat/input/v2" "github.com/elastic/elastic-agent-libs/logp" + "github.com/elastic/elastic-agent-libs/logp/logptest" ) //go:embed testdata/corner-cases.json @@ -71,7 +72,7 @@ func TestEventWithNonStringData(t *testing.T) { var jdEvent []byte func TestRestartsJournalctlOnError(t *testing.T) { - logp.DevelopmentSetup(logp.ToObserverOutput()) + logger, observedLogs := logptest.NewTestingLoggerWithObserver(t, "") ctx := context.Background() mock := JctlMock{ @@ -100,7 +101,7 @@ func TestRestartsJournalctlOnError(t *testing.T) { return &mock, nil } - reader, err := New(logp.L(), ctx, nil, nil, nil, journalfield.IncludeMatches{}, []int{}, SeekHead, "", 0, "", factory) + reader, err := New(logger, ctx, nil, nil, nil, journalfield.IncludeMatches{}, []int{}, SeekHead, "", 0, "", factory) if err != nil { t.Fatalf("cannot instantiate journalctl reader: %s", err) } @@ -129,7 +130,7 @@ func TestRestartsJournalctlOnError(t *testing.T) { // - reader error: 'journalctl exited with code 42', restarting... // - starting new mock journalclt ID: 2 - logs := logp.ObserverLogs().TakeAll() + logs := observedLogs.TakeAll() if len(logs) != 3 { t.Fatalf("expecting 3 log lines from 'input.journald.reader.journalctl-runner', got %d", len(logs)) } diff --git a/heartbeat/monitors/logger/logger_test.go b/heartbeat/monitors/logger/logger_test.go index 18952f2652da..cbd1fd173746 100644 --- a/heartbeat/monitors/logger/logger_test.go +++ b/heartbeat/monitors/logger/logger_test.go @@ -24,13 +24,12 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.uber.org/zap" - "go.uber.org/zap/zapcore" - "go.uber.org/zap/zaptest/observer" "github.com/elastic/beats/v7/heartbeat/eventext" "github.com/elastic/beats/v7/heartbeat/monitors/wrappers/summarizer/jobsummary" "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/elastic-agent-libs/logp" + "github.com/elastic/elastic-agent-libs/logp/logptest" "github.com/elastic/elastic-agent-libs/mapstr" ) @@ -57,10 +56,8 @@ func generateFakeNetworkInfo() NetworkInfo { func TestLogRun(t *testing.T) { t.Run("should log the monitor completion", func(t *testing.T) { - core, observed := observer.New(zapcore.InfoLevel) - SetLogger(logp.NewLogger("t", zap.WrapCore(func(in zapcore.Core) zapcore.Core { - return zapcore.NewTee(in, core) - }))) + log, observed := logptest.NewTestingLoggerWithObserver(t, "t") + SetLogger(log) durationUs := int64(5000 * time.Microsecond) steps := 1337 @@ -97,10 +94,8 @@ func TestLogRun(t *testing.T) { }) t.Run("should log network information if available", func(t *testing.T) { - core, observed := observer.New(zapcore.InfoLevel) - SetLogger(logp.NewLogger("t", zap.WrapCore(func(in zapcore.Core) zapcore.Core { - return zapcore.NewTee(in, core) - }))) + log, observed := logptest.NewTestingLoggerWithObserver(t, "t") + SetLogger(log) durationUs := int64(5000 * time.Microsecond) steps := 1337 diff --git a/heartbeat/monitors/wrappers/wrappers_test.go b/heartbeat/monitors/wrappers/wrappers_test.go index eb4565334342..b9a32cda7e10 100644 --- a/heartbeat/monitors/wrappers/wrappers_test.go +++ b/heartbeat/monitors/wrappers/wrappers_test.go @@ -27,10 +27,10 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.uber.org/zap" - "go.uber.org/zap/zapcore" "go.uber.org/zap/zaptest/observer" "github.com/elastic/elastic-agent-libs/logp" + "github.com/elastic/elastic-agent-libs/logp/logptest" "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-lookslike" "github.com/elastic/go-lookslike/isdef" @@ -78,10 +78,8 @@ func testCommonWrap(t *testing.T, tt testDef) { t.Run(tt.name, func(t *testing.T) { wrapped := WrapCommon(tt.jobs, tt.sFields, nil) - core, observedLogs := observer.New(zapcore.InfoLevel) - logger.SetLogger(logp.NewLogger("t", zap.WrapCore(func(in zapcore.Core) zapcore.Core { - return zapcore.NewTee(in, core) - }))) + log, observedLogs := logptest.NewTestingLoggerWithObserver(t, "t") + logger.SetLogger(log) results, err := jobs.ExecJobsAndConts(t, wrapped) assert.NoError(t, err) diff --git a/libbeat/autodiscover/autodiscover_test.go b/libbeat/autodiscover/autodiscover_test.go index 281b58752ead..a72b76ce675a 100644 --- a/libbeat/autodiscover/autodiscover_test.go +++ b/libbeat/autodiscover/autodiscover_test.go @@ -19,7 +19,6 @@ package autodiscover import ( "bytes" - "encoding/json" "errors" "fmt" "path/filepath" @@ -34,7 +33,6 @@ import ( "github.com/stretchr/testify/require" "go.uber.org/zap" "go.uber.org/zap/zapcore" - "go.uber.org/zap/zaptest/observer" "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/cfgfile" @@ -179,7 +177,6 @@ func TestNilAutodiscover(t *testing.T) { } func TestAutodiscover(t *testing.T) { - printDebugLogsOnFailure(t) goroutines := resources.NewGoroutinesChecker() defer goroutines.Check(t) @@ -656,7 +653,6 @@ func TestAutodiscoverWithMutlipleEntries(t *testing.T) { } func TestAutodiscoverDebounce(t *testing.T) { - printDebugLogsOnFailure(t) // Register mock autodiscover provider busChan := make(chan bus.Bus, 1) Registry = NewRegistry() @@ -755,26 +751,6 @@ func TestAutodiscoverDebounce(t *testing.T) { requireRunningRunners(t, autodiscover, 0) } -func printDebugLogsOnFailure(t *testing.T) { - observed, zapLogs := observer.New(zapcore.DebugLevel) - _, err := logp.ConfigureWithCoreLocal(logp.Config{}, observed) - require.NoError(t, err) - - t.Cleanup(func() { - if t.Failed() { - t.Logf("Debug Logs:\n") - for _, log := range zapLogs.TakeAll() { - data, err := json.Marshal(log) - if err != nil { - t.Errorf("failed encoding log as JSON: %s", err) - } - t.Logf("%s", string(data)) - } - return - } - }) -} - func requireRunningRunners(t *testing.T, autodiscover *Autodiscover, nRunners int) { t.Helper() nRunnersStr := strings.Builder{} diff --git a/libbeat/monitoring/report/log/log_test.go b/libbeat/monitoring/report/log/log_test.go index 3063e9e068f6..32c66e3cc107 100644 --- a/libbeat/monitoring/report/log/log_test.go +++ b/libbeat/monitoring/report/log/log_test.go @@ -21,13 +21,9 @@ import ( "testing" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - "go.uber.org/zap/zapcore" - "go.uber.org/zap/zaptest/observer" "github.com/elastic/beats/v7/libbeat/beat" conf "github.com/elastic/elastic-agent-libs/config" - "github.com/elastic/elastic-agent-libs/logp" "github.com/elastic/elastic-agent-libs/logp/logptest" "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/elastic-agent-libs/monitoring" @@ -82,9 +78,7 @@ func TestMakeDeltaSnapshot(t *testing.T) { } func TestReporterLog(t *testing.T) { - observed, zapLogs := observer.New(zapcore.DebugLevel) - logger, err := logp.ConfigureWithCoreLocal(logp.Config{}, observed) - require.NoError(t, err) + logger, zapLogs := logptest.NewTestingLoggerWithObserver(t, "") reporter := reporter{config: defaultConfig(), logger: logger.Named("monitoring")} diff --git a/packetbeat/npcap/npcap_test.go b/packetbeat/npcap/npcap_test.go index bd5c499820b2..719ce390cbf2 100644 --- a/packetbeat/npcap/npcap_test.go +++ b/packetbeat/npcap/npcap_test.go @@ -24,16 +24,10 @@ import ( "runtime" "testing" - "github.com/elastic/elastic-agent-libs/logp" + "github.com/elastic/elastic-agent-libs/logp/logptest" ) func TestNpcap(t *testing.T) { - // Ugh. - var lcfg logp.Config - logp.ToObserverOutput()(&lcfg) - logp.Configure(lcfg) - obs := logp.ObserverLogs() - // Working space. path := filepath.Join(t.TempDir(), "installer") if runtime.GOOS == "windows" { @@ -46,22 +40,18 @@ func TestNpcap(t *testing.T) { if err != nil { t.Fatalf("failed to build mock installer: %v\n%s", err, b) } - log := logp.NewLogger("npcap_test_install") + log := logptest.NewTestingLogger(t, "npcap_test_install") for _, compat := range []bool{false, true} { for _, dst := range []string{ "", // Default. `C:\some\other\location`, } { err = install(context.Background(), log, path, dst, compat) - messages := obs.TakeAll() if err != nil { if dst == "" { dst = "default location" } t.Errorf("unexpected error running installer to %s with compat=%t: %v", dst, compat, err) - for _, e := range messages { - t.Log(e.Message) - } } } } @@ -74,14 +64,10 @@ func TestNpcap(t *testing.T) { if err != nil { t.Fatalf("failed to build mock uninstaller: %v\n%s", err, b) } - log := logp.NewLogger("npcap_test_uninstall") + log := logptest.NewTestingLogger(t, "npcap_test_install") err = uninstall(context.Background(), log, path) - messages := obs.TakeAll() if err != nil { t.Errorf("unexpected error running uninstaller: %v", err) - for _, e := range messages { - t.Log(e.Message) - } } }) } diff --git a/x-pack/metricbeat/module/gcp/metrics/metrics_requester_test.go b/x-pack/metricbeat/module/gcp/metrics/metrics_requester_test.go index 20f8a5d9e366..5879c1ce7614 100644 --- a/x-pack/metricbeat/module/gcp/metrics/metrics_requester_test.go +++ b/x-pack/metricbeat/module/gcp/metrics/metrics_requester_test.go @@ -14,6 +14,7 @@ import ( "github.com/elastic/beats/v7/x-pack/metricbeat/module/gcp" "github.com/elastic/elastic-agent-libs/logp" + "github.com/elastic/elastic-agent-libs/logp/logptest" ) func TestGetTimeIntervalAligner(t *testing.T) { @@ -76,9 +77,6 @@ func TestGetTimeIntervalAligner(t *testing.T) { } func TestGetFilterForMetric(t *testing.T) { - logp.DevelopmentSetup(logp.ToObserverOutput()) - var logger = logp.NewLogger("TestGetFilterForMetric") - cases := []struct { title string s string @@ -90,68 +88,67 @@ func TestGetFilterForMetric(t *testing.T) { "compute service with empty config", gcp.ServiceCompute, "", - metricsRequester{config: config{}, logger: logger}, + metricsRequester{config: config{}}, "metric.type=\"dummy\"", }, { "compute service with configured region", gcp.ServiceCompute, "", - metricsRequester{config: config{Region: "foo"}, logger: logger}, + metricsRequester{config: config{Region: "foo"}}, "metric.type=\"dummy\" AND resource.labels.zone = starts_with(\"foo\")", }, { "compute service with configured zone", gcp.ServiceCompute, "", - metricsRequester{config: config{Zone: "foo"}, logger: logger}, + metricsRequester{config: config{Zone: "foo"}}, "metric.type=\"dummy\" AND resource.labels.zone = starts_with(\"foo\")", }, { "compute service with configured regions", gcp.ServiceCompute, "", - metricsRequester{config: config{Regions: []string{"foo", "bar"}}, logger: logger}, + metricsRequester{config: config{Regions: []string{"foo", "bar"}}}, "metric.type=\"dummy\" AND (resource.labels.zone = starts_with(\"foo\") OR resource.labels.zone = starts_with(\"bar\"))", }, { "compute service with configured region and zone", gcp.ServiceCompute, "", - metricsRequester{config: config{Region: "foo", Zone: "bar"}, logger: logger}, + metricsRequester{config: config{Region: "foo", Zone: "bar"}}, "metric.type=\"dummy\" AND resource.labels.zone = starts_with(\"foo\")", }, { "compute service with configured region and regions", gcp.ServiceCompute, "", - metricsRequester{config: config{Region: "foobar", Regions: []string{"foo", "bar"}}, logger: logger}, + metricsRequester{config: config{Region: "foobar", Regions: []string{"foo", "bar"}}}, "metric.type=\"dummy\" AND resource.labels.zone = starts_with(\"foobar\")", }, { "aiplatform service with configured region and zone", "aiplatform", "", - metricsRequester{config: config{Region: "foo", Zone: "bar", LocationLabel: "resource.label.location"}, logger: logger}, + metricsRequester{config: config{Region: "foo", Zone: "bar", LocationLabel: "resource.label.location"}}, "metric.type=\"dummy\" AND resource.label.location = starts_with(\"foo\")", }, } for _, c := range cases { t.Run(c.title, func(t *testing.T) { + logger, observedLogs := logptest.NewTestingLoggerWithObserver(t, "TestGetFilterForMetric") + c.r.logger = logger filter := c.r.getFilterForMetric(c.s, "dummy") assert.Equal(t, c.expectedFilter, filter) // NOTE: test that we output a log message with the filter value, as this is **extremely** // useful to debug issues and we want to make sure is being done. - logs := logp.ObserverLogs(). + logs := observedLogs. FilterLevelExact(zapcore.DebugLevel). // we are OK it being logged at debug level FilterMessageSnippet(filter). Len() assert.Equal(t, logs, 1) - // NOTE: cleanup observed logs at each iteration to start with no messages. - _ = logp.ObserverLogs().TakeAll() - }) } }