From 546eff6d8ff98124ada532ac2c33aeaaa1bc867d Mon Sep 17 00:00:00 2001 From: Blake Rouse Date: Mon, 27 Jan 2020 14:00:13 -0500 Subject: [PATCH] Fix issue where default go logger is not discarded when either * or stdout is selected. (#15707) * Disable default logger when stdlog or the default all selector is not enabled. * Fix issue where default go logger is not discarded when either * or stdout is selected. Fix #10251. * Change logic to always discard unless in debug and when all or stdlog is selected. Add more tests. * Fix comments. * Add changelog entry. (cherry picked from commit 1838734b4d95d6f8042c9466bf34d917d03bcbbd) --- CHANGELOG.next.asciidoc | 2 ++ libbeat/logp/core.go | 17 ++++++++++++----- libbeat/logp/core_test.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index b2d051abf8e..3b40f74ac82 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -64,6 +64,8 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d - Fix missing output in dockerlogbeat {pull}15719[15719] - Fix logging target settings being ignored when Beats are started via systemd or docker. {issue}12024[12024] {pull}15422[15442] - Do not load dashboards where not available. {pull}15802[15802] +- Fix issue where default go logger is not discarded when either * or stdout is selected. {issue}10251[10251] {pull}15708[15708] + *Auditbeat* diff --git a/libbeat/logp/core.go b/libbeat/logp/core.go index 8e5fa6e7c8b..5dd990714f4 100644 --- a/libbeat/logp/core.go +++ b/libbeat/logp/core.go @@ -37,7 +37,8 @@ import ( ) var ( - _log unsafe.Pointer // Pointer to a coreLogger. Access via atomic.LoadPointer. + _log unsafe.Pointer // Pointer to a coreLogger. Access via atomic.LoadPointer. + _defaultGoLog = golog.Writer() ) func init() { @@ -86,6 +87,10 @@ func Configure(cfg Config) error { return errors.Wrap(err, "failed to build log output") } + // Default logger is always discard, debug level below will + // possibly re-enable it. + golog.SetOutput(ioutil.Discard) + // Enabled selectors when debug is enabled. selectors := make(map[string]struct{}, len(cfg.Selectors)) if cfg.Level.Enabled(DebugLevel) && len(cfg.Selectors) > 0 { @@ -98,10 +103,12 @@ func Configure(cfg Config) error { selectors["*"] = struct{}{} } - if _, enabled := selectors["stdlog"]; !enabled { - // Disable standard logging by default (this is sometimes used by - // libraries and we don't want their spam). - golog.SetOutput(ioutil.Discard) + // Re-enable the default go logger output when either stdlog + // or all selector is enabled. + _, stdlogEnabled := selectors["stdlog"] + _, allEnabled := selectors["*"] + if stdlogEnabled || allEnabled { + golog.SetOutput(_defaultGoLog) } sink = selectiveWrapper(sink, selectors) diff --git a/libbeat/logp/core_test.go b/libbeat/logp/core_test.go index a619731a7de..b7d838097f1 100644 --- a/libbeat/logp/core_test.go +++ b/libbeat/logp/core_test.go @@ -18,6 +18,8 @@ package logp import ( + "io/ioutil" + golog "log" "strings" "testing" @@ -232,3 +234,31 @@ func TestL(t *testing.T) { assert.Equal(t, "warning 1", log.Message) } } + +func TestDebugAllStdoutEnablesDefaultGoLogger(t *testing.T) { + DevelopmentSetup(WithSelectors("*")) + assert.Equal(t, _defaultGoLog, golog.Writer()) + + DevelopmentSetup(WithSelectors("stdlog")) + assert.Equal(t, _defaultGoLog, golog.Writer()) + + DevelopmentSetup(WithSelectors("*", "stdlog")) + assert.Equal(t, _defaultGoLog, golog.Writer()) + + DevelopmentSetup(WithSelectors("other")) + assert.Equal(t, ioutil.Discard, golog.Writer()) +} + +func TestNotDebugAllStdoutDisablesDefaultGoLogger(t *testing.T) { + DevelopmentSetup(WithSelectors("*"), WithLevel(InfoLevel)) + assert.Equal(t, ioutil.Discard, golog.Writer()) + + DevelopmentSetup(WithSelectors("stdlog"), WithLevel(InfoLevel)) + assert.Equal(t, ioutil.Discard, golog.Writer()) + + DevelopmentSetup(WithSelectors("*", "stdlog"), WithLevel(InfoLevel)) + assert.Equal(t, ioutil.Discard, golog.Writer()) + + DevelopmentSetup(WithSelectors("other"), WithLevel(InfoLevel)) + assert.Equal(t, ioutil.Discard, golog.Writer()) +}