| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package zap_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"io" |
| 10 |
"testing" |
| 11 |
"time" |
| 12 |
|
| 13 |
"go.uber.org/zap" |
| 14 |
"go.uber.org/zap/zapcore" |
| 15 |
"golang.org/x/exp/event/eventtest" |
| 16 |
) |
| 17 |
|
| 18 |
var ( |
| 19 |
zapLog = eventtest.Hooks{ |
| 20 |
AStart: func(ctx context.Context, a int) context.Context { |
| 21 |
zapCtx(ctx).Info(eventtest.A.Msg, zap.Int(eventtest.A.Name, a)) |
| 22 |
return ctx |
| 23 |
}, |
| 24 |
AEnd: func(ctx context.Context) {}, |
| 25 |
BStart: func(ctx context.Context, b string) context.Context { |
| 26 |
zapCtx(ctx).Info(eventtest.B.Msg, zap.String(eventtest.B.Name, b)) |
| 27 |
return ctx |
| 28 |
}, |
| 29 |
BEnd: func(ctx context.Context) {}, |
| 30 |
} |
| 31 |
zapLogf = eventtest.Hooks{ |
| 32 |
AStart: func(ctx context.Context, a int) context.Context { |
| 33 |
zapCtx(ctx).Sugar().Infof(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 |
zapCtx(ctx).Sugar().Infof(eventtest.B.Msgf, b) |
| 39 |
return ctx |
| 40 |
}, |
| 41 |
BEnd: func(ctx context.Context) {}, |
| 42 |
} |
| 43 |
) |
| 44 |
|
| 45 |
type zapKey struct{} |
| 46 |
|
| 47 |
func zapCtx(ctx context.Context) *zap.Logger { |
| 48 |
return ctx.Value(zapKey{}).(*zap.Logger) |
| 49 |
} |
| 50 |
|
| 51 |
func zapPrint(w io.Writer) context.Context { |
| 52 |
now := eventtest.ExporterOptions().Now |
| 53 |
ec := zap.NewProductionEncoderConfig() |
| 54 |
ec.EncodeDuration = zapcore.NanosDurationEncoder |
| 55 |
timeEncoder := zapcore.TimeEncoderOfLayout(eventtest.TimeFormat) |
| 56 |
ec.EncodeTime = func(_ time.Time, a zapcore.PrimitiveArrayEncoder) { |
| 57 |
timeEncoder(now(), a) |
| 58 |
} |
| 59 |
enc := zapcore.NewConsoleEncoder(ec) |
| 60 |
logger := zap.New(zapcore.NewCore( |
| 61 |
enc, |
| 62 |
zapcore.AddSync(w), |
| 63 |
zap.InfoLevel, |
| 64 |
)) |
| 65 |
return context.WithValue(context.Background(), zapKey{}, logger) |
| 66 |
} |
| 67 |
|
| 68 |
func BenchmarkZapLogDiscard(b *testing.B) { |
| 69 |
eventtest.RunBenchmark(b, zapPrint(io.Discard), zapLog) |
| 70 |
} |
| 71 |
|
| 72 |
func BenchmarkZapLogfDiscard(b *testing.B) { |
| 73 |
eventtest.RunBenchmark(b, zapPrint(io.Discard), zapLogf) |
| 74 |
} |
| 75 |
|
| 76 |
func TestZapLogfDiscard(t *testing.T) { |
| 77 |
eventtest.TestBenchmark(t, zapPrint, zapLogf, ` |
| 78 |
2020/03/05 14:27:48 info a where A=0 |
| 79 |
2020/03/05 14:27:49 info b where B="A value" |
| 80 |
2020/03/05 14:27:50 info a where A=1 |
| 81 |
2020/03/05 14:27:51 info b where B="Some other value" |
| 82 |
2020/03/05 14:27:52 info a where A=22 |
| 83 |
2020/03/05 14:27:53 info b where B="Some other value" |
| 84 |
2020/03/05 14:27:54 info a where A=333 |
| 85 |
2020/03/05 14:27:55 info b where B=" " |
| 86 |
2020/03/05 14:27:56 info a where A=4444 |
| 87 |
2020/03/05 14:27:57 info b where B="prime count of values" |
| 88 |
2020/03/05 14:27:58 info a where A=55555 |
| 89 |
2020/03/05 14:27:59 info b where B="V" |
| 90 |
2020/03/05 14:28:00 info a where A=666666 |
| 91 |
2020/03/05 14:28:01 info b where B="A value" |
| 92 |
2020/03/05 14:28:02 info a where A=7777777 |
| 93 |
2020/03/05 14:28:03 info b where B="A value" |
| 94 |
`) |
| 95 |
} |
| 96 |
func TestLogZap(t *testing.T) { |
| 97 |
eventtest.TestBenchmark(t, zapPrint, zapLog, ` |
| 98 |
2020/03/05 14:27:48 info a {"A": 0} |
| 99 |
2020/03/05 14:27:49 info b {"B": "A value"} |
| 100 |
2020/03/05 14:27:50 info a {"A": 1} |
| 101 |
2020/03/05 14:27:51 info b {"B": "Some other value"} |
| 102 |
2020/03/05 14:27:52 info a {"A": 22} |
| 103 |
2020/03/05 14:27:53 info b {"B": "Some other value"} |
| 104 |
2020/03/05 14:27:54 info a {"A": 333} |
| 105 |
2020/03/05 14:27:55 info b {"B": " "} |
| 106 |
2020/03/05 14:27:56 info a {"A": 4444} |
| 107 |
2020/03/05 14:27:57 info b {"B": "prime count of values"} |
| 108 |
2020/03/05 14:27:58 info a {"A": 55555} |
| 109 |
2020/03/05 14:27:59 info b {"B": "V"} |
| 110 |
2020/03/05 14:28:00 info a {"A": 666666} |
| 111 |
2020/03/05 14:28:01 info b {"B": "A value"} |
| 112 |
2020/03/05 14:28:02 info a {"A": 7777777} |
| 113 |
2020/03/05 14:28:03 info b {"B": "A value"} |
| 114 |
`) |
| 115 |
} |