-
Notifications
You must be signed in to change notification settings - Fork 911
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
glog: add context variants to most log functions
We export this new API to make the internal and external versions identical. The context is currently plumbed through to the internal/logsink package, but effectively discarded there. cl/560684897 (google-internal) cl/579771826 (google-internal)
- Loading branch information
Showing
4 changed files
with
376 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package glog | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"testing" | ||
|
||
"github.com/golang/glog/internal/logsink" | ||
) | ||
|
||
type contextKey string | ||
type fakeLogSink struct { | ||
context context.Context | ||
} | ||
|
||
var ctxKey = contextKey("key") | ||
var ctxValue = "some-value" | ||
var originalSinks = logsink.StructuredSinks | ||
|
||
func (s *fakeLogSink) Printf(meta *logsink.Meta, format string, args ...any) (int, error) { | ||
s.context = meta.Context | ||
return 0, nil | ||
} | ||
|
||
// Test that log.(Info|Error|Warning)Context functions behave the same as non context variants | ||
// and pass right context. | ||
func TestLogContext(t *testing.T) { | ||
fakeLogSink := &fakeLogSink{} | ||
logsink.StructuredSinks = append([]logsink.Structured{fakeLogSink}, originalSinks...) | ||
|
||
funcs := map[string]func(ctx context.Context, args ...any){ | ||
"InfoContext": InfoContext, | ||
"InfoContextDepth": func(ctx context.Context, args ...any) { InfoContextDepth(ctx, 2, args) }, | ||
"ErrorContext": ErrorContext, | ||
"WarningContext": WarningContext, | ||
} | ||
|
||
ctx := context.WithValue(context.Background(), ctxKey, ctxValue) | ||
for name, f := range funcs { | ||
f(ctx, "test") | ||
want := ctxValue | ||
if got := fakeLogSink.context.Value(ctxKey); got != want { | ||
t.Errorf("%s: context value unexpectedly missing: got %q, want %q", name, got, want) | ||
} | ||
} | ||
} | ||
|
||
// Test that V.InfoContext behaves the same as V.Info and passes right context. | ||
func TestVInfoContext(t *testing.T) { | ||
fakeLogSink := &fakeLogSink{} | ||
logsink.StructuredSinks = append([]logsink.Structured{fakeLogSink}, originalSinks...) | ||
if err := flag.Lookup("v").Value.Set("2"); err != nil { | ||
t.Fatalf("Failed to set -v=2: %v", err) | ||
} | ||
defer flag.Lookup("v").Value.Set("0") | ||
ctx := context.WithValue(context.Background(), ctxKey, ctxValue) | ||
V(2).InfoContext(ctx, "test") | ||
want := ctxValue | ||
if got := fakeLogSink.context.Value(ctxKey); got != want { | ||
t.Errorf("V.InfoContext: context value unexpectedly missing: got %q, want %q", got, want) | ||
} | ||
} |
Oops, something went wrong.