forked from golang/glog
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This implements WithCallDepth from logr v0.4.0 in klogr.
- Loading branch information
Showing
5 changed files
with
96 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ module k8s.io/klog/v2 | |
|
||
go 1.13 | ||
|
||
require github.com/go-logr/logr v0.2.1 | ||
require github.com/go-logr/logr v0.4.0 |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
github.com/go-logr/logr v0.2.1 h1:fV3MLmabKIZ383XifUjFSwcoGee0v9qgPp8wy5svibE= | ||
github.com/go-logr/logr v0.2.1/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU= | ||
github.com/go-logr/logr v0.4.0 h1:K7/B1jt6fIBQVd4Owv2MqGQClcgf0R266+7C/QjRcLc= | ||
github.com/go-logr/logr v0.4.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU= |
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,16 @@ | ||
package calldepth | ||
|
||
import ( | ||
"github.com/go-logr/logr" | ||
) | ||
|
||
// Putting these functions into a separate file makes it possible to validate that | ||
// their source code file is *not* logged because of WithCallDepth(1). | ||
|
||
func myInfo(l logr.Logger, msg string) { | ||
logr.WithCallDepth(l, 1).Info(msg) | ||
} | ||
|
||
func myInfo2(l logr.Logger, msg string) { | ||
myInfo(logr.WithCallDepth(l, 1), msg) | ||
} |
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,57 @@ | ||
// Package calldepth does black-box testing. | ||
// | ||
// Another intentional effect is that "go test" compiles | ||
// this into a separate binary which we need because | ||
// we have to configure klog differently that TestOutput. | ||
|
||
package calldepth | ||
|
||
import ( | ||
"bytes" | ||
"flag" | ||
"strings" | ||
"testing" | ||
|
||
"k8s.io/klog/v2" | ||
"k8s.io/klog/v2/klogr" | ||
) | ||
|
||
func TestCallDepth(t *testing.T) { | ||
klog.InitFlags(nil) | ||
flag.CommandLine.Set("v", "10") | ||
flag.CommandLine.Set("skip_headers", "false") | ||
flag.CommandLine.Set("logtostderr", "false") | ||
flag.CommandLine.Set("alsologtostderr", "false") | ||
flag.CommandLine.Set("stderrthreshold", "10") | ||
flag.Parse() | ||
|
||
t.Run("call-depth", func(t *testing.T) { | ||
logr := klogr.New() | ||
|
||
// hijack the klog output | ||
tmpWriteBuffer := bytes.NewBuffer(nil) | ||
klog.SetOutput(tmpWriteBuffer) | ||
|
||
validate := func(t *testing.T) { | ||
output := tmpWriteBuffer.String() | ||
if !strings.Contains(output, "call_depth_main_test.go:") { | ||
t.Fatalf("output should have contained call_depth_main_test.go, got instead: %s", output) | ||
} | ||
} | ||
|
||
t.Run("direct", func(t *testing.T) { | ||
logr.Info("hello world") | ||
validate(t) | ||
}) | ||
|
||
t.Run("indirect", func(t *testing.T) { | ||
myInfo(logr, "hello world") | ||
validate(t) | ||
}) | ||
|
||
t.Run("nested", func(t *testing.T) { | ||
myInfo2(logr, "hello world") | ||
validate(t) | ||
}) | ||
}) | ||
} |
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