Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split global and normal logger tests #15700

Merged
merged 3 commits into from
Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 0 additions & 116 deletions libbeat/logp/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package logp

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -27,8 +26,6 @@ import (

func TestLogger(t *testing.T) {
exerciseLogger := func() {
Info("unnamed global logger")

log := NewLogger("example")
log.Info("some message")
log.Infof("some message with parameter x=%v, y=%v", 1, 2)
Expand All @@ -53,78 +50,6 @@ func TestLogger(t *testing.T) {
exerciseLogger()
}

func TestLoggerSelectors(t *testing.T) {
if err := DevelopmentSetup(WithSelectors("good", " padded "), ToObserverOutput()); err != nil {
t.Fatal(err)
}

assert.True(t, HasSelector("padded"))

good := NewLogger("good")
bad := NewLogger("bad")

good.Debug("is logged")
logs := ObserverLogs().TakeAll()
assert.Len(t, logs, 1)

// Selectors only apply to debug level logs.
bad.Debug("not logged")
logs = ObserverLogs().TakeAll()
assert.Len(t, logs, 0)

bad.Info("is also logged")
logs = ObserverLogs().TakeAll()
assert.Len(t, logs, 1)
}

func TestGlobalLoggerLevel(t *testing.T) {
if err := DevelopmentSetup(ToObserverOutput()); err != nil {
t.Fatal(err)
}

const loggerName = "tester"

Debug(loggerName, "debug")
logs := ObserverLogs().TakeAll()
if assert.Len(t, logs, 1) {
assert.Equal(t, zap.DebugLevel, logs[0].Level)
assert.Equal(t, loggerName, logs[0].LoggerName)
assert.Equal(t, "debug", logs[0].Message)
}

Info("info")
logs = ObserverLogs().TakeAll()
if assert.Len(t, logs, 1) {
assert.Equal(t, zap.InfoLevel, logs[0].Level)
assert.Equal(t, "", logs[0].LoggerName)
assert.Equal(t, "info", logs[0].Message)
}

Warn("warning")
logs = ObserverLogs().TakeAll()
if assert.Len(t, logs, 1) {
assert.Equal(t, zap.WarnLevel, logs[0].Level)
assert.Equal(t, "", logs[0].LoggerName)
assert.Equal(t, "warning", logs[0].Message)
}

Err("error")
logs = ObserverLogs().TakeAll()
if assert.Len(t, logs, 1) {
assert.Equal(t, zap.ErrorLevel, logs[0].Level)
assert.Equal(t, "", logs[0].LoggerName)
assert.Equal(t, "error", logs[0].Message)
}

Critical("critical")
logs = ObserverLogs().TakeAll()
if assert.Len(t, logs, 1) {
assert.Equal(t, zap.ErrorLevel, logs[0].Level)
assert.Equal(t, "", logs[0].LoggerName)
assert.Equal(t, "critical", logs[0].Message)
}
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests have been moved to global_test.go


func TestLoggerLevel(t *testing.T) {
if err := DevelopmentSetup(ToObserverOutput()); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -166,47 +91,6 @@ func TestLoggerLevel(t *testing.T) {
}
}

func TestRecover(t *testing.T) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests have been moved to global_test.go

const recoveryExplanation = "Something went wrong"
const cause = "unexpected condition"

DevelopmentSetup(ToObserverOutput())

defer func() {
logs := ObserverLogs().TakeAll()
if assert.Len(t, logs, 1) {
log := logs[0]
assert.Equal(t, zap.ErrorLevel, log.Level)
assert.Equal(t, "logp/core_test.go",
strings.Split(log.Caller.TrimmedPath(), ":")[0])
assert.Contains(t, log.Message, recoveryExplanation+
". Recovering, but please report this.")
assert.Contains(t, log.ContextMap(), "panic")
}
}()

defer Recover(recoveryExplanation)
panic(cause)
}

func TestHasSelector(t *testing.T) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test has been moved to selective_test.go

DevelopmentSetup(WithSelectors("*", "config"))
assert.True(t, HasSelector("config"))
assert.False(t, HasSelector("publish"))
}

func TestIsDebug(t *testing.T) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests have been moved to global_test.go

DevelopmentSetup()
assert.True(t, IsDebug("all"))

DevelopmentSetup(WithSelectors("*"))
assert.True(t, IsDebug("all"))

DevelopmentSetup(WithSelectors("only_this"))
assert.False(t, IsDebug("all"))
assert.True(t, IsDebug("only_this"))
}

func TestL(t *testing.T) {
if err := DevelopmentSetup(ToObserverOutput()); err != nil {
t.Fatal(err)
Expand Down
8 changes: 2 additions & 6 deletions libbeat/logp/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// specific language governing permissions and limitations
// under the License.

//+build !nologpglobal

package logp

import (
Expand All @@ -31,12 +33,6 @@ func MakeDebug(selector string) func(string, ...interface{}) {
}
}

// HasSelector returns true if the given selector was explicitly set.
func HasSelector(selector string) bool {
_, found := loadLogger().selectors[selector]
return found
}

// IsDebug returns true if the given selector would be logged.
// Deprecated: Use logp.NewLogger.
func IsDebug(selector string) bool {
Expand Down
111 changes: 111 additions & 0 deletions libbeat/logp/global_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

//+build !nologpglobal

package logp

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
"go.uber.org/zap"
)

func TestGlobalLoggerLevel(t *testing.T) {
if err := DevelopmentSetup(ToObserverOutput()); err != nil {
t.Fatal(err)
}

const loggerName = "tester"

Debug(loggerName, "debug")
logs := ObserverLogs().TakeAll()
if assert.Len(t, logs, 1) {
assert.Equal(t, zap.DebugLevel, logs[0].Level)
assert.Equal(t, loggerName, logs[0].LoggerName)
assert.Equal(t, "debug", logs[0].Message)
}

Info("info")
logs = ObserverLogs().TakeAll()
if assert.Len(t, logs, 1) {
assert.Equal(t, zap.InfoLevel, logs[0].Level)
assert.Equal(t, "", logs[0].LoggerName)
assert.Equal(t, "info", logs[0].Message)
}

Warn("warning")
logs = ObserverLogs().TakeAll()
if assert.Len(t, logs, 1) {
assert.Equal(t, zap.WarnLevel, logs[0].Level)
assert.Equal(t, "", logs[0].LoggerName)
assert.Equal(t, "warning", logs[0].Message)
}

Err("error")
logs = ObserverLogs().TakeAll()
if assert.Len(t, logs, 1) {
assert.Equal(t, zap.ErrorLevel, logs[0].Level)
assert.Equal(t, "", logs[0].LoggerName)
assert.Equal(t, "error", logs[0].Message)
}

Critical("critical")
logs = ObserverLogs().TakeAll()
if assert.Len(t, logs, 1) {
assert.Equal(t, zap.ErrorLevel, logs[0].Level)
assert.Equal(t, "", logs[0].LoggerName)
assert.Equal(t, "critical", logs[0].Message)
}
}

func TestRecover(t *testing.T) {
const recoveryExplanation = "Something went wrong"
const cause = "unexpected condition"

DevelopmentSetup(ToObserverOutput())

defer func() {
logs := ObserverLogs().TakeAll()
if assert.Len(t, logs, 1) {
log := logs[0]
assert.Equal(t, zap.ErrorLevel, log.Level)
assert.Equal(t, "logp/global_test.go",
strings.Split(log.Caller.TrimmedPath(), ":")[0])
assert.Contains(t, log.Message, recoveryExplanation+
". Recovering, but please report this.")
assert.Contains(t, log.ContextMap(), "panic")
}
}()

defer Recover(recoveryExplanation)
panic(cause)
}

func TestIsDebug(t *testing.T) {
DevelopmentSetup()
assert.True(t, IsDebug("all"))

DevelopmentSetup(WithSelectors("*"))
assert.True(t, IsDebug("all"))

DevelopmentSetup(WithSelectors("only_this"))
assert.False(t, IsDebug("all"))
assert.True(t, IsDebug("only_this"))
}
10 changes: 10 additions & 0 deletions libbeat/logp/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package logp

import (
"fmt"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
Expand Down Expand Up @@ -203,6 +205,14 @@ func (l *Logger) DPanicw(msg string, keysAndValues ...interface{}) {
l.sugar.DPanicw(msg, keysAndValues...)
}

// Recover stops a panicking goroutine and logs an Error.
func (l *Logger) Recover(msg string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like this is called anywhere yet (including tests), should it be? Or is it just for use in followups?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is for the followups.

if r := recover(); r != nil {
msg := fmt.Sprintf("%s. Recovering, but please report this.", msg)
l.Error(msg, zap.Any("panic", r), zap.Stack("stack"))
}
}

// L returns an unnamed global logger.
func L() *Logger {
return loadLogger().logger
Expand Down
6 changes: 6 additions & 0 deletions libbeat/logp/selective.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ type selectiveCore struct {
core zapcore.Core
}

// HasSelector returns true if the given selector was explicitly set.
func HasSelector(selector string) bool {
_, found := loadLogger().selectors[selector]
return found
}

func selectiveWrapper(core zapcore.Core, selectors map[string]struct{}) zapcore.Core {
if len(selectors) == 0 {
return core
Expand Down
54 changes: 54 additions & 0 deletions libbeat/logp/selective_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package logp

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestHasSelector(t *testing.T) {
DevelopmentSetup(WithSelectors("*", "config"))
assert.True(t, HasSelector("config"))
assert.False(t, HasSelector("publish"))
}

func TestLoggerSelectors(t *testing.T) {
if err := DevelopmentSetup(WithSelectors("good", " padded "), ToObserverOutput()); err != nil {
t.Fatal(err)
}

assert.True(t, HasSelector("padded"))

good := NewLogger("good")
bad := NewLogger("bad")

good.Debug("is logged")
logs := ObserverLogs().TakeAll()
assert.Len(t, logs, 1)

// Selectors only apply to debug level logs.
bad.Debug("not logged")
logs = ObserverLogs().TakeAll()
assert.Len(t, logs, 0)

bad.Info("is also logged")
logs = ObserverLogs().TakeAll()
assert.Len(t, logs, 1)
}