| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package gokit_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"fmt" |
| 10 |
"io" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/go-kit/kit/log" |
| 14 |
"golang.org/x/exp/event/adapter/logfmt" |
| 15 |
"golang.org/x/exp/event/eventtest" |
| 16 |
) |
| 17 |
|
| 18 |
var ( |
| 19 |
gokitLog = eventtest.Hooks{ |
| 20 |
AStart: func(ctx context.Context, a int) context.Context { |
| 21 |
gokitCtx(ctx).Log(eventtest.A.Name, a, "msg", eventtest.A.Msg) |
| 22 |
return ctx |
| 23 |
}, |
| 24 |
AEnd: func(ctx context.Context) {}, |
| 25 |
BStart: func(ctx context.Context, b string) context.Context { |
| 26 |
gokitCtx(ctx).Log(eventtest.B.Name, b, "msg", eventtest.B.Msg) |
| 27 |
return ctx |
| 28 |
}, |
| 29 |
BEnd: func(ctx context.Context) {}, |
| 30 |
} |
| 31 |
gokitLogf = eventtest.Hooks{ |
| 32 |
AStart: func(ctx context.Context, a int) context.Context { |
| 33 |
gokitCtx(ctx).Log("msg", fmt.Sprintf(eventtest.A.Msgf, a)) |
| 34 |
return ctx |
| 35 |
}, |
| 36 |
AEnd: func(ctx context.Context) {}, |
| 37 |
BStart: func(ctx context.Context, b string) context.Context { |
| 38 |
gokitCtx(ctx).Log("msg", fmt.Sprintf(eventtest.B.Msgf, b)) |
| 39 |
return ctx |
| 40 |
}, |
| 41 |
BEnd: func(ctx context.Context) {}, |
| 42 |
} |
| 43 |
) |
| 44 |
|
| 45 |
type gokitKey struct{} |
| 46 |
|
| 47 |
func gokitCtx(ctx context.Context) log.Logger { |
| 48 |
return ctx.Value(gokitKey{}).(log.Logger) |
| 49 |
} |
| 50 |
|
| 51 |
func gokitPrint(w io.Writer) context.Context { |
| 52 |
logger := log.NewLogfmtLogger(log.NewSyncWriter(w)) |
| 53 |
now := eventtest.ExporterOptions().Now |
| 54 |
logger = log.With(logger, "time", log.TimestampFormat(now, logfmt.TimeFormat), "level", "info") |
| 55 |
return context.WithValue(context.Background(), gokitKey{}, logger) |
| 56 |
} |
| 57 |
|
| 58 |
func BenchmarkGokitLogDiscard(b *testing.B) { |
| 59 |
eventtest.RunBenchmark(b, gokitPrint(io.Discard), gokitLog) |
| 60 |
} |
| 61 |
|
| 62 |
func BenchmarkGokitLogfDiscard(b *testing.B) { |
| 63 |
eventtest.RunBenchmark(b, gokitPrint(io.Discard), gokitLogf) |
| 64 |
} |
| 65 |
|
| 66 |
func TestGokitLogfDiscard(t *testing.T) { |
| 67 |
eventtest.TestBenchmark(t, gokitPrint, gokitLogf, eventtest.LogfOutput) |
| 68 |
} |
| 69 |
func TestLogGokit(t *testing.T) { |
| 70 |
eventtest.TestBenchmark(t, gokitPrint, gokitLog, eventtest.LogfmtOutput) |
| 71 |
} |