Skip to content

Commit

Permalink
Fix issue where default go logger is not discarded when either * or s…
Browse files Browse the repository at this point in the history
…tdout is selected. (#15708)

* 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.
  • Loading branch information
blakerouse authored Jan 27, 2020
1 parent 0e57f6b commit 1838734
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Do not load dashboards where not available. {pull}15802[15802]
- Fix issue where TLS settings would be ignored when a forward proxy was in use. {pull}15516{15516}
- Update replicaset group to apps/v1 {pull}15854[15802]
- Fix issue where default go logger is not discarded when either * or stdout is selected. {issue}10251[10251] {pull}15708[15708]


*Auditbeat*

Expand Down
17 changes: 12 additions & 5 deletions libbeat/logp/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions libbeat/logp/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package logp

import (
"io/ioutil"
golog "log"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -116,3 +118,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())
}

0 comments on commit 1838734

Please sign in to comment.