Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change introduces minimum log level support. GO_DISCOVERY_LOG_LEVEL config can be used to set the log level. When set, no logs below GO_DISCOVERY_LOG_LEVEL will be printed. Possible log levels are debug, info, error, fatal. Log severity sequence is debug < info < error < fatal. In case of invalid/empty log level, it print all logs. Fixes golang/go#40339 Change-Id: I66116e6778eb6386bf1e8df6947078a78fece691 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/245797 Reviewed-by: Julie Qiu <julie@golang.org>
- Loading branch information
1 parent
ff5b79a
commit 3ad2709
Showing
7 changed files
with
184 additions
and
4 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
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
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,121 @@ | ||
// Copyright 2019 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package log | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"cloud.google.com/go/logging" | ||
) | ||
|
||
const ( | ||
debugMsg = "debugMsg" | ||
infoMsg = "infoMsg" | ||
errorMsg = "errorMsg" | ||
) | ||
|
||
// Do not run in parallel. It overrides currentLevel. | ||
func TestSetLogLevel(t *testing.T) { | ||
oldLevel := getLevel() | ||
defer func() { currentLevel = oldLevel }() | ||
|
||
tests := []struct { | ||
name string | ||
newLevel string | ||
wantLevel logging.Severity | ||
}{ | ||
{name: "default level", newLevel: "", wantLevel: logging.Default}, | ||
{name: "invalid level", newLevel: "xyz", wantLevel: logging.Default}, | ||
{name: "debug level", newLevel: "debug", wantLevel: logging.Debug}, | ||
{name: "info level", newLevel: "info", wantLevel: logging.Info}, | ||
{name: "error level", newLevel: "error", wantLevel: logging.Error}, | ||
{name: "fatal level", newLevel: "fatal", wantLevel: logging.Critical}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
SetLevel(test.newLevel) | ||
gotLevel := getLevel() | ||
if test.wantLevel != gotLevel { | ||
t.Errorf("Error: want=%s, got=%s", test.wantLevel, gotLevel) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// Do not run in parallel. It overrides logger with mockLogger. | ||
func TestLogLevel(t *testing.T) { | ||
oldLogger := logger | ||
defer func() { logger = oldLogger }() | ||
logger = &mockLogger{} | ||
|
||
// logs below info(like debug) won't print | ||
SetLevel("info") | ||
|
||
tests := []struct { | ||
name string | ||
logFunc func(context.Context, interface{}) | ||
logMsg string | ||
expected bool | ||
}{ | ||
{name: "debug", logFunc: Debug, logMsg: debugMsg, expected: false}, | ||
{name: "info", logFunc: Info, logMsg: infoMsg, expected: true}, | ||
{name: "error", logFunc: Error, logMsg: errorMsg, expected: true}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
|
||
test.logFunc(context.Background(), test.logMsg) | ||
logs := logger.(*mockLogger).logs | ||
got := strings.Contains(logs, test.logMsg) | ||
|
||
if got != test.expected { | ||
t.Errorf("expected : %v, got %v", test.expected, got) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// Do not run in parallel. It overrides logger with mockLogger. | ||
func TestDefaultLogLevel(t *testing.T) { | ||
oldLogger := logger | ||
defer func() { logger = oldLogger }() | ||
logger = &mockLogger{} | ||
|
||
SetLevel("") // default behaviour; print everything | ||
|
||
tests := []struct { | ||
name string | ||
logFunc func(context.Context, interface{}) | ||
logMsg string | ||
}{ | ||
{name: "debug", logFunc: Debug, logMsg: debugMsg}, | ||
{name: "info", logFunc: Info, logMsg: infoMsg}, | ||
{name: "error", logFunc: Error, logMsg: errorMsg}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
|
||
test.logFunc(context.Background(), test.logMsg) | ||
logs := logger.(*mockLogger).logs | ||
|
||
if !strings.Contains(logs, test.logMsg) { | ||
t.Errorf("%v not logged.", test.logMsg) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
type mockLogger struct { | ||
logs string | ||
} | ||
|
||
func (l *mockLogger) log(ctx context.Context, s logging.Severity, payload interface{}) { | ||
l.logs += fmt.Sprintf("%s: %+v", s, payload) | ||
} |