slog: remove FromContext, NewContext and Ctx
Change-Id: I5adbfa4e62df03e8a3bb588750ed2e283ee394a2
Reviewed-on: https://go-review.googlesource.com/c/exp/+/461940
Reviewed-by: Alan Donovan <adonovan@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
diff --git a/slog/context.go b/slog/context.go
deleted file mode 100644
index 504adaf..0000000
--- a/slog/context.go
+++ /dev/null
@@ -1,30 +0,0 @@
-// Copyright 2022 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 slog
-
-import "context"
-
-type contextKey struct{}
-
-// NewContext returns a context that contains the given Logger.
-// Use FromContext to retrieve the Logger.
-func NewContext(ctx context.Context, l *Logger) context.Context {
- return context.WithValue(ctx, contextKey{}, l)
-}
-
-// FromContext returns the Logger stored in ctx by NewContext, or the default
-// Logger if there is none.
-func FromContext(ctx context.Context) *Logger {
- if l, ok := ctx.Value(contextKey{}).(*Logger); ok {
- return l
- }
- return Default()
-}
-
-// Ctx retrieves a Logger from the given context using FromContext. Then it adds
-// the given context to the Logger using WithContext and returns the result.
-func Ctx(ctx context.Context) *Logger {
- return FromContext(ctx).WithContext(ctx)
-}
diff --git a/slog/context_test.go b/slog/context_test.go
deleted file mode 100644
index 60df74b..0000000
--- a/slog/context_test.go
+++ /dev/null
@@ -1,49 +0,0 @@
-// Copyright 2022 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 slog
-
-import (
- "context"
- "testing"
-)
-
-func TestContext(t *testing.T) {
- // If there is no Logger in the context, FromContext returns the default
- // Logger.
- ctx := context.Background()
- gotl := FromContext(ctx)
- if _, ok := gotl.Handler().(*defaultHandler); !ok {
- t.Error("did not get default Logger")
- }
-
- // If there is a Logger in the context, FromContext returns it, with the ctx
- // arg.
- h := &captureHandler{}
- ctx = NewContext(ctx, New(h))
- ctx = context.WithValue(ctx, "ID", 1)
- gotl = FromContext(ctx)
- if gotl.Handler() != h {
- t.Fatal("wrong handler")
- }
- // FromContext preserves the context of the Logger that was stored
- // with NewContext, in this case nil.
- gotl.Info("")
- if g := h.r.Context; g != nil {
- t.Errorf("got %v, want nil", g)
- }
- gotl = Ctx(ctx)
- if gotl.Handler() != h {
- t.Fatal("wrong handler")
- }
- // Ctx adds the argument context to the Logger.
- gotl.Info("")
- c := h.r.Context
- if c == nil {
- t.Fatal("got nil Context")
- }
- if g, w := c.Value("ID"), 1; g != w {
- t.Errorf("got ID %v, want %v", g, w)
- }
-}
diff --git a/slog/logger_test.go b/slog/logger_test.go
index c574b00..3c94a43 100644
--- a/slog/logger_test.go
+++ b/slog/logger_test.go
@@ -512,13 +512,4 @@
}
})
})
- b.Run("Ctx", func(b *testing.B) {
- dl := Default()
- SetDefault(l)
- defer SetDefault(dl)
- b.ReportAllocs()
- for i := 0; i < b.N; i++ {
- Ctx(ctx).LogAttrs(LevelInfo, "msg", Int("a", 1), String("b", "two"), Bool("c", true))
- }
- })
}