sdk: add TestLogLevel for setting log level in tests

And default log level to WARN.
This commit is contained in:
Daniel Nephin 2022-02-03 13:40:19 -05:00
parent 5b9bf6ec63
commit 1a9a656a7f
8 changed files with 24 additions and 12 deletions

View File

@ -67,7 +67,7 @@ func NewTestACLAgent(t *testing.T, name string, hcl string, resolveAuthz authzRe
bd.Logger = hclog.NewInterceptLogger(&hclog.LoggerOptions{
Name: name,
Level: hclog.Debug,
Level: testutil.TestLogLevel,
Output: logBuffer,
TimeFormat: "04:05.000",
})

View File

@ -8,10 +8,11 @@ import (
"testing"
"time"
"github.com/hashicorp/consul/lib"
"github.com/hashicorp/consul/sdk/testutil"
"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/assert"
"github.com/hashicorp/consul/lib"
"github.com/hashicorp/consul/sdk/testutil"
)
func TestAE_scaleFactor(t *testing.T) {
@ -401,7 +402,6 @@ func (m *mock) SyncChanges() error {
func testSyncer(t *testing.T) *StateSyncer {
logger := hclog.New(&hclog.LoggerOptions{
Level: 0,
Output: testutil.NewLogBuffer(t),
})

View File

@ -507,7 +507,7 @@ func newDefaultDeps(t *testing.T, c *Config) Deps {
logger := hclog.NewInterceptLogger(&hclog.LoggerOptions{
Name: c.NodeName,
Level: hclog.Debug,
Level: testutil.TestLogLevel,
Output: testutil.NewLogBuffer(t),
})

View File

@ -1445,7 +1445,7 @@ func TestLeader_ConfigEntryBootstrap_Fail(t *testing.T) {
logger := hclog.NewInterceptLogger(&hclog.LoggerOptions{
Name: config.NodeName,
Level: hclog.Debug,
Level: testutil.TestLogLevel,
Output: io.MultiWriter(pw, testutil.NewLogBuffer(t)),
})

View File

@ -21,6 +21,7 @@ import (
"github.com/hashicorp/consul/agent/structs"
tokenStore "github.com/hashicorp/consul/agent/token"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/sdk/testutil"
"github.com/hashicorp/consul/sdk/testutil/retry"
"github.com/hashicorp/consul/testrpc"
"github.com/hashicorp/consul/types"
@ -2760,7 +2761,7 @@ func (m *mockQueryServer) GetLogger() hclog.Logger {
m.Logger = hclog.New(&hclog.LoggerOptions{
Name: "mock_query",
Output: m.LogBuffer,
Level: hclog.Debug,
Level: testutil.TestLogLevel,
})
}
return m.Logger

View File

@ -25,10 +25,7 @@ func init() {
func GetBufferedLogger() hclog.Logger {
localLogBuffer = new(bytes.Buffer)
return hclog.New(&hclog.LoggerOptions{
Level: 0,
Output: localLogBuffer,
})
return hclog.New(&hclog.LoggerOptions{Output: localLogBuffer})
}
type fauxConnPool struct {

View File

@ -159,7 +159,7 @@ func (a *TestAgent) Start(t *testing.T) error {
}
logger := hclog.NewInterceptLogger(&hclog.LoggerOptions{
Level: hclog.Debug,
Level: testutil.TestLogLevel,
Output: logOutput,
TimeFormat: "04:05.000",
Name: name,

View File

@ -10,6 +10,20 @@ import (
"github.com/hashicorp/go-hclog"
)
// TestLogLevel is set from the TEST_LOG_LEVEL environment variable. It can
// be used by tests to set the log level of a hclog.Logger. Defaults to
// hclog.Warn if the environment variable is unset, or if the value of the
// environment variable can not be matched to a log level.
var TestLogLevel = testLogLevel()
func testLogLevel() hclog.Level {
level := hclog.LevelFromString(os.Getenv("TEST_LOG_LEVEL"))
if level != hclog.NoLevel {
return level
}
return hclog.Warn
}
func Logger(t TestingTB) hclog.InterceptLogger {
return LoggerWithOutput(t, NewLogBuffer(t))
}