-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split global and normal logger tests (#15700)
Move logp global functions to global.go and selective.go, plus split up tests. The change also adds the `nologpglobal` build tag to global.go and global_test.go. By default all global functions are available. When compiling or running a package its test code with `-tags=nologpglobal`, then global logp functions are not available. (cherry picked from commit 4183ba3)
- Loading branch information
Steffen Siering
committed
Jan 28, 2020
1 parent
e0d629c
commit 76a119a
Showing
6 changed files
with
183 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |