Package list golang-github-go-kit-kit / 055e4ba
log: Unexport Context and change With and WithPrefix to top level functions. Chris Hines 4 years ago
25 changed file(s) with 101 addition(s) and 121 deletion(s). Raw diff Collapse all Expand all
0 package main
0 ´╗┐package main
11
22 import (
33 "context"
5050 var logger log.Logger
5151 {
5252 logger = log.NewLogfmtLogger(os.Stdout)
53 logger = log.NewContext(logger).With("ts", log.DefaultTimestampUTC)
54 logger = log.NewContext(logger).With("caller", log.DefaultCaller)
53 logger = log.With(logger, "ts", log.DefaultTimestampUTC)
54 logger = log.With(logger, "caller", log.DefaultCaller)
5555 }
5656 logger.Log("msg", "hello")
5757 defer logger.Log("msg", "goodbye")
8585 var tracer stdopentracing.Tracer
8686 {
8787 if *zipkinAddr != "" {
88 logger := log.NewContext(logger).With("tracer", "ZipkinHTTP")
88 logger := log.With(logger, "tracer", "ZipkinHTTP")
8989 logger.Log("addr", *zipkinAddr)
9090
9191 // endpoint typically looks like: http://zipkinhost:9411/api/v1/spans
104104 os.Exit(1)
105105 }
106106 } else if *zipkinKafkaAddr != "" {
107 logger := log.NewContext(logger).With("tracer", "ZipkinKafka")
107 logger := log.With(logger, "tracer", "ZipkinKafka")
108108 logger.Log("addr", *zipkinKafkaAddr)
109109
110110 collector, err := zipkin.NewKafkaCollector(
125125 os.Exit(1)
126126 }
127127 } else if *appdashAddr != "" {
128 logger := log.NewContext(logger).With("tracer", "Appdash")
128 logger := log.With(logger, "tracer", "Appdash")
129129 logger.Log("addr", *appdashAddr)
130130 tracer = appdashot.NewTracer(appdash.NewRemoteCollector(*appdashAddr))
131131 } else if *lightstepToken != "" {
132 logger := log.NewContext(logger).With("tracer", "LightStep")
132 logger := log.With(logger, "tracer", "LightStep")
133133 logger.Log() // probably don't want to print out the token :)
134134 tracer = lightstep.NewTracer(lightstep.Options{
135135 AccessToken: *lightstepToken,
136136 })
137137 defer lightstep.FlushLightStepTracer(tracer)
138138 } else {
139 logger := log.NewContext(logger).With("tracer", "none")
139 logger := log.With(logger, "tracer", "none")
140140 logger.Log()
141141 tracer = stdopentracing.GlobalTracer() // no-op
142142 }
154154 var sumEndpoint endpoint.Endpoint
155155 {
156156 sumDuration := duration.With("method", "Sum")
157 sumLogger := log.NewContext(logger).With("method", "Sum")
157 sumLogger := log.With(logger, "method", "Sum")
158158
159159 sumEndpoint = addsvc.MakeSumEndpoint(service)
160160 sumEndpoint = opentracing.TraceServer(tracer, "Sum")(sumEndpoint)
164164 var concatEndpoint endpoint.Endpoint
165165 {
166166 concatDuration := duration.With("method", "Concat")
167 concatLogger := log.NewContext(logger).With("method", "Concat")
167 concatLogger := log.With(logger, "method", "Concat")
168168
169169 concatEndpoint = addsvc.MakeConcatEndpoint(service)
170170 concatEndpoint = opentracing.TraceServer(tracer, "Concat")(concatEndpoint)
189189
190190 // Debug listener.
191191 go func() {
192 logger := log.NewContext(logger).With("transport", "debug")
192 logger := log.With(logger, "transport", "debug")
193193
194194 m := http.NewServeMux()
195195 m.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
205205
206206 // HTTP transport.
207207 go func() {
208 logger := log.NewContext(logger).With("transport", "HTTP")
208 logger := log.With(logger, "transport", "HTTP")
209209 h := addsvc.MakeHTTPHandler(endpoints, tracer, logger)
210210 logger.Log("addr", *httpAddr)
211211 errc <- http.ListenAndServe(*httpAddr, h)
213213
214214 // gRPC transport.
215215 go func() {
216 logger := log.NewContext(logger).With("transport", "gRPC")
216 logger := log.With(logger, "transport", "gRPC")
217217
218218 ln, err := net.Listen("tcp", *grpcAddr)
219219 if err != nil {
231231
232232 // Thrift transport.
233233 go func() {
234 logger := log.NewContext(logger).With("transport", "Thrift")
234 logger := log.With(logger, "transport", "Thrift")
235235
236236 var protocolFactory thrift.TProtocolFactory
237237 switch *thriftProtocol {
4343 var logger log.Logger
4444 {
4545 logger = log.NewLogfmtLogger(os.Stderr)
46 logger = log.NewContext(logger).With("ts", log.DefaultTimestampUTC)
47 logger = log.NewContext(logger).With("caller", log.DefaultCaller)
46 logger = log.With(logger, "ts", log.DefaultTimestampUTC)
47 logger = log.With(logger, "caller", log.DefaultCaller)
4848 }
4949
5050 // Service discovery domain. In this example we use Consul.
0 package main
0 ´╗┐package main
11
22 import (
33 "flag"
2020 var logger log.Logger
2121 {
2222 logger = log.NewLogfmtLogger(os.Stderr)
23 logger = log.NewContext(logger).With("ts", log.DefaultTimestampUTC)
24 logger = log.NewContext(logger).With("caller", log.DefaultCaller)
23 logger = log.With(logger, "ts", log.DefaultTimestampUTC)
24 logger = log.With(logger, "caller", log.DefaultCaller)
2525 }
2626
2727 var s profilesvc.Service
3232
3333 var h http.Handler
3434 {
35 h = profilesvc.MakeHTTPHandler(s, log.NewContext(logger).With("component", "HTTP"))
35 h = profilesvc.MakeHTTPHandler(s, log.With(logger, "component", "HTTP"))
3636 }
3737
3838 errs := make(chan error)
4646 var logger log.Logger
4747 logger = log.NewLogfmtLogger(os.Stderr)
4848 logger = &serializedLogger{Logger: logger}
49 logger = log.NewContext(logger).With("ts", log.DefaultTimestampUTC)
49 logger = log.With(logger, "ts", log.DefaultTimestampUTC)
5050
5151 var (
5252 cargos = inmem.NewCargoRepository()
7777
7878 var bs booking.Service
7979 bs = booking.NewService(cargos, locations, handlingEvents, rs)
80 bs = booking.NewLoggingService(log.NewContext(logger).With("component", "booking"), bs)
80 bs = booking.NewLoggingService(log.With(logger, "component", "booking"), bs)
8181 bs = booking.NewInstrumentingService(
8282 kitprometheus.NewCounterFrom(stdprometheus.CounterOpts{
8383 Namespace: "api",
9696
9797 var ts tracking.Service
9898 ts = tracking.NewService(cargos, handlingEvents)
99 ts = tracking.NewLoggingService(log.NewContext(logger).With("component", "tracking"), ts)
99 ts = tracking.NewLoggingService(log.With(logger, "component", "tracking"), ts)
100100 ts = tracking.NewInstrumentingService(
101101 kitprometheus.NewCounterFrom(stdprometheus.CounterOpts{
102102 Namespace: "api",
115115
116116 var hs handling.Service
117117 hs = handling.NewService(handlingEvents, handlingEventFactory, handlingEventHandler)
118 hs = handling.NewLoggingService(log.NewContext(logger).With("component", "handling"), hs)
118 hs = handling.NewLoggingService(log.With(logger, "component", "handling"), hs)
119119 hs = handling.NewInstrumentingService(
120120 kitprometheus.NewCounterFrom(stdprometheus.CounterOpts{
121121 Namespace: "api",
132132 hs,
133133 )
134134
135 httpLogger := log.NewContext(logger).With("component", "http")
135 httpLogger := log.With(logger, "component", "http")
136136
137137 mux := http.NewServeMux()
138138
2121
2222 var logger log.Logger
2323 logger = log.NewLogfmtLogger(os.Stderr)
24 logger = log.NewContext(logger).With("listen", *listen).With("caller", log.DefaultCaller)
24 logger = log.With(logger, "listen", *listen, "caller", log.DefaultCaller)
2525
2626 fieldKeys := []string{"method", "error"}
2727 requestCount := kitprometheus.NewCounterFrom(stdprometheus.CounterOpts{
66 )
77
88 func benchmarkRunner(b *testing.B, logger log.Logger, f func(log.Logger)) {
9 lc := log.NewContext(logger).With("common_key", "common_value")
9 lc := log.With(logger, "common_key", "common_value")
1010 b.ReportAllocs()
1111 b.ResetTimer()
1212 for i := 0; i < b.N; i++ {
1616
1717 var (
1818 baseMessage = func(logger log.Logger) { logger.Log("foo_key", "foo_value") }
19 withMessage = func(logger log.Logger) { log.NewContext(logger).With("a", "b").Log("c", "d") }
19 withMessage = func(logger log.Logger) { log.With(logger, "a", "b").Log("c", "d") }
2020 )
66 // want a different set of levels, you can create your own levels type very
77 // easily, and you can elide the configuration.
88 type Levels struct {
9 ctx *log.Context
9 logger log.Logger
1010 levelKey string
1111
1212 // We have a choice between storing level values in string fields or
3333 // New creates a new leveled logger, wrapping the passed logger.
3434 func New(logger log.Logger, options ...Option) Levels {
3535 l := Levels{
36 ctx: log.NewContext(logger),
36 logger: logger,
3737 levelKey: "level",
3838
3939 debugValue: "debug",
5151 // With returns a new leveled logger that includes keyvals in all log events.
5252 func (l Levels) With(keyvals ...interface{}) Levels {
5353 return Levels{
54 ctx: l.ctx.With(keyvals...),
54 logger: log.With(l.logger, keyvals...),
5555 levelKey: l.levelKey,
5656 debugValue: l.debugValue,
5757 infoValue: l.infoValue,
6363
6464 // Debug returns a debug level logger.
6565 func (l Levels) Debug() log.Logger {
66 return l.ctx.WithPrefix(l.levelKey, l.debugValue)
66 return log.WithPrefix(l.logger, l.levelKey, l.debugValue)
6767 }
6868
6969 // Info returns an info level logger.
7070 func (l Levels) Info() log.Logger {
71 return l.ctx.WithPrefix(l.levelKey, l.infoValue)
71 return log.WithPrefix(l.logger, l.levelKey, l.infoValue)
7272 }
7373
7474 // Warn returns a warning level logger.
7575 func (l Levels) Warn() log.Logger {
76 return l.ctx.WithPrefix(l.levelKey, l.warnValue)
76 return log.WithPrefix(l.logger, l.levelKey, l.warnValue)
7777 }
7878
7979 // Error returns an error level logger.
8080 func (l Levels) Error() log.Logger {
81 return l.ctx.WithPrefix(l.levelKey, l.errorValue)
81 return log.WithPrefix(l.logger, l.levelKey, l.errorValue)
8282 }
8383
8484 // Crit returns a critical level logger.
8585 func (l Levels) Crit() log.Logger {
86 return l.ctx.WithPrefix(l.levelKey, l.critValue)
86 return log.WithPrefix(l.logger, l.levelKey, l.critValue)
8787 }
8888
8989 // Option sets a parameter for leveled loggers.
4141 // resulting log output. We can use a context to improve the RunTask example.
4242 //
4343 // func RunTask(task Task, logger log.Logger) string {
44 // logger = log.NewContext(logger).With("taskID", task.ID)
44 // logger = log.With(logger, "taskID", task.ID)
4545 // logger.Log("event", "starting task")
4646 // ...
4747 // taskHelper(task.Cmd, logger)
7171 // entries contain a timestamp and source location looks like this:
7272 //
7373 // logger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stdout))
74 // logger = log.NewContext(logger).With("ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
74 // logger = log.With(logger, "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
7575 //
7676 // Concurrent Safety
7777 //
4242 }
4343
4444 RunTask := func(task Task, logger log.Logger) {
45 logger = log.NewContext(logger).With("taskID", task.ID)
45 logger = log.With(logger, "taskID", task.ID)
4646 logger.Log("event", "starting task")
4747
4848 taskHelper(task.Cmd, logger)
6767 return count
6868 }
6969
70 logger = log.NewContext(logger).With("count", log.Valuer(counter))
70 logger = log.With(logger, "count", log.Valuer(counter))
7171
7272 logger.Log("call", "first")
7373 logger.Log("call", "second")
8787 return baseTime
8888 }
8989
90 logger = log.NewContext(logger).With("time", log.Timestamp(mockTime), "caller", log.DefaultCaller)
90 logger = log.With(logger, "time", log.Timestamp(mockTime), "caller", log.DefaultCaller)
9191
9292 logger.Log("call", "first")
9393 logger.Log("call", "second")
1212 t.Parallel()
1313 buf := &bytes.Buffer{}
1414 logger := log.NewJSONLogger(buf)
15 logger = log.NewContext(logger).With("caller", log.DefaultCaller)
15 logger = log.With(logger, "caller", log.DefaultCaller)
1616
1717 if err := logger.Log(); err != nil {
1818 t.Fatal(err)
1616 return l
1717 }},
1818 {"TimeContext", func(l log.Logger) log.Logger {
19 return log.NewContext(l).With("time", log.DefaultTimestampUTC)
19 return log.With(l, "time", log.DefaultTimestampUTC)
2020 }},
2121 {"CallerContext", func(l log.Logger) log.Logger {
22 return log.NewContext(l).With("caller", log.DefaultCaller)
22 return log.With(l, "caller", log.DefaultCaller)
2323 }},
2424 {"TimeCallerReqIDContext", func(l log.Logger) log.Logger {
25 return log.NewContext(l).With("time", log.DefaultTimestampUTC, "caller", log.DefaultCaller, "reqID", 29)
25 return log.With(l, "time", log.DefaultTimestampUTC, "caller", log.DefaultCaller, "reqID", 29)
2626 }},
2727 }
2828
1111 // setup logger with level filter
1212 logger := log.NewLogfmtLogger(os.Stdout)
1313 logger = level.NewFilter(logger, level.AllowInfo())
14 logger = log.NewContext(logger).With("caller", log.DefaultCaller)
14 logger = log.With(logger, "caller", log.DefaultCaller)
1515
1616 // use level helpers to log at different levels
1717 level.Error(logger).Log("err", errors.New("bad data"))
33
44 // Error returns a logger that includes a Key/ErrorValue pair.
55 func Error(logger log.Logger) log.Logger {
6 return log.NewContext(logger).WithPrefix(Key(), ErrorValue())
6 return log.WithPrefix(logger, Key(), ErrorValue())
77 }
88
99 // Warn returns a logger that includes a Key/WarnValue pair.
1010 func Warn(logger log.Logger) log.Logger {
11 return log.NewContext(logger).WithPrefix(Key(), WarnValue())
11 return log.WithPrefix(logger, Key(), WarnValue())
1212 }
1313
1414 // Info returns a logger that includes a Key/InfoValue pair.
1515 func Info(logger log.Logger) log.Logger {
16 return log.NewContext(logger).WithPrefix(Key(), InfoValue())
16 return log.WithPrefix(logger, Key(), InfoValue())
1717 }
1818
1919 // Debug returns a logger that includes a Key/DebugValue pair.
2020 func Debug(logger log.Logger) log.Logger {
21 return log.NewContext(logger).WithPrefix(Key(), DebugValue())
21 return log.WithPrefix(logger, Key(), DebugValue())
2222 }
2323
2424 // NewFilter wraps next and implements level filtering. See the commentary on
143143 var logger log.Logger
144144 logger = log.NewLogfmtLogger(&buf)
145145 logger = level.NewFilter(logger, level.AllowAll())
146 logger = log.NewContext(logger).With("caller", log.DefaultCaller)
146 logger = log.With(logger, "caller", log.DefaultCaller)
147147
148148 level.Info(logger).Log("foo", "bar")
149149 if want, have := `level=info caller=level_test.go:149 foo=bar`, strings.TrimSpace(buf.String()); want != have {
158158 // to specify a higher callstack depth value.
159159 var logger log.Logger
160160 logger = log.NewLogfmtLogger(&buf)
161 logger = log.NewContext(logger).With("caller", log.Caller(5))
161 logger = log.With(logger, "caller", log.Caller(5))
162162 logger = level.NewFilter(logger, level.AllowAll())
163163
164164 level.Info(logger).Log("foo", "bar")
1414 // the missing value.
1515 var ErrMissingValue = errors.New("(MISSING)")
1616
17 // NewContext returns a new Context that logs to logger.
18 func NewContext(logger Logger) *Context {
19 if c, ok := logger.(*Context); ok {
17 // newContext returns a new context that logs to logger.
18 func newContext(logger Logger) *context {
19 if c, ok := logger.(*context); ok {
2020 return c
2121 }
22 return &Context{logger: logger}
22 return &context{logger: logger}
2323 }
2424
2525 // Context must always have the same number of stack frames between calls to
5656 // Context.Log through a variable with type Context. Using pointer receivers
5757 // avoids this problem.
5858
59 // A Context wraps a Logger and holds keyvals that it includes in all log
60 // events. When logging, a Context replaces all value elements (odd indexes)
59 // A context wraps a Logger and holds keyvals that it includes in all log
60 // events. When logging, a context replaces all value elements (odd indexes)
6161 // containing a Valuer with their generated value for each call to its Log
6262 // method.
63 type Context struct {
63 type context struct {
6464 logger Logger
6565 keyvals []interface{}
6666 hasValuer bool
6969 // Log replaces all value elements (odd indexes) containing a Valuer in the
7070 // stored context with their generated value, appends keyvals, and passes the
7171 // result to the wrapped Logger.
72 func (l *Context) Log(keyvals ...interface{}) error {
72 func (l *context) Log(keyvals ...interface{}) error {
7373 kvs := append(l.keyvals, keyvals...)
7474 if len(kvs)%2 != 0 {
7575 kvs = append(kvs, ErrMissingValue)
8585 return l.logger.Log(kvs...)
8686 }
8787
88 // With returns a new Context with keyvals appended to those of the receiver.
89 func (l *Context) With(keyvals ...interface{}) *Context {
88 // With returns a new context with keyvals appended to those of the receiver.
89 func With(logger Logger, keyvals ...interface{}) Logger {
9090 if len(keyvals) == 0 {
91 return l
91 return logger
9292 }
93 l := newContext(logger)
9394 kvs := append(l.keyvals, keyvals...)
9495 if len(kvs)%2 != 0 {
9596 kvs = append(kvs, ErrMissingValue)
9697 }
97 return &Context{
98 return &context{
9899 logger: l.logger,
99100 // Limiting the capacity of the stored keyvals ensures that a new
100101 // backing array is created if the slice must grow in Log or With.
105106 }
106107 }
107108
108 // WithPrefix returns a new Context with keyvals prepended to those of the
109 // WithPrefix returns a new context with keyvals prepended to those of the
109110 // receiver.
110 func (l *Context) WithPrefix(keyvals ...interface{}) *Context {
111 func WithPrefix(logger Logger, keyvals ...interface{}) Logger {
111112 if len(keyvals) == 0 {
112 return l
113 return logger
113114 }
115 l := newContext(logger)
114116 // Limiting the capacity of the stored keyvals ensures that a new
115117 // backing array is created if the slice must grow in Log or With.
116118 // Using the extra capacity without copying risks a data race that
125127 kvs = append(kvs, ErrMissingValue)
126128 }
127129 kvs = append(kvs, l.keyvals...)
128 return &Context{
130 return &context{
129131 logger: l.logger,
130132 keyvals: kvs,
131133 hasValuer: l.hasValuer || containsValuer(keyvals),
1515 logger := log.NewLogfmtLogger(buf)
1616
1717 kvs := []interface{}{"a", 123}
18 lc := log.NewContext(logger).With(kvs...)
18 lc := log.With(logger, kvs...)
1919 kvs[1] = 0 // With should copy its key values
2020
21 lc = lc.With("b", "c") // With should stack
21 lc = log.With(lc, "b", "c") // With should stack
2222 if err := lc.Log("msg", "message"); err != nil {
2323 t.Fatal(err)
2424 }
2727 }
2828
2929 buf.Reset()
30 lc = lc.WithPrefix("p", "first")
30 lc = log.WithPrefix(lc, "p", "first")
3131 if err := lc.Log("msg", "message"); err != nil {
3232 t.Fatal(err)
3333 }
4444 return nil
4545 }))
4646
47 lc := log.NewContext(logger)
48
49 lc.Log("k")
50 if want, have := 2, len(output); want != have {
51 t.Errorf("want len(output) == %v, have %v", want, have)
52 }
53 if want, have := log.ErrMissingValue, output[1]; want != have {
54 t.Errorf("want %#v, have %#v", want, have)
55 }
56
57 lc.With("k1").WithPrefix("k0").Log("k2")
47 log.WithPrefix(log.With(logger, "k1"), "k0").Log("k2")
5848 if want, have := 6, len(output); want != have {
5949 t.Errorf("want len(output) == %v, have %v", want, have)
6050 }
6656 }
6757
6858 // Test that Context.Log has a consistent function stack depth when binding
69 // log.Valuers, regardless of how many times Context.With has been called or
70 // whether Context.Log is called via an interface typed variable or a concrete
71 // typed variable.
59 // log.Valuers, regardless of how many times Context.With has been called.
7260 func TestContextStackDepth(t *testing.T) {
7361 t.Parallel()
7462 fn := fmt.Sprintf("%n", stack.Caller(0))
9078 return nil
9179 })
9280
93 concrete := log.NewContext(logger).With("stack", stackValuer)
94 var iface log.Logger = concrete
81 logger = log.With(logger, "stack", stackValuer)
9582
9683 // Call through interface to get baseline.
97 iface.Log("k", "v")
84 logger.Log("k", "v")
9885 want := output[1].(int)
9986
10087 for len(output) < 10 {
101 concrete.Log("k", "v")
88 logger.Log("k", "v")
10289 if have := output[1]; have != want {
10390 t.Errorf("%d Withs: have %v, want %v", len(output)/2-1, have, want)
10491 }
10592
106 iface.Log("k", "v")
107 if have := output[1]; have != want {
108 t.Errorf("%d Withs: have %v, want %v", len(output)/2-1, have, want)
109 }
110
111 wrapped := log.NewContext(concrete)
93 wrapped := log.With(logger)
11294 wrapped.Log("k", "v")
11395 if have := output[1]; have != want {
11496 t.Errorf("%d Withs: have %v, want %v", len(output)/2-1, have, want)
11597 }
11698
117 concrete = concrete.With("k", "v")
118 iface = concrete
99 logger = log.With(logger, "k", "v")
119100 }
120101 }
121102
139120
140121 // With must be careful about handling slices that can grow without
141122 // copying the underlying array, so give it a challenge.
142 l := log.NewContext(logger).With(make([]interface{}, 0, 2)...)
123 l := log.With(logger, make([]interface{}, 0, 2)...)
143124
144125 // Start logging concurrently. Each goroutine logs its id so the logger
145126 // can bucket the event counts.
174155
175156 func BenchmarkOneWith(b *testing.B) {
176157 logger := log.NewNopLogger()
177 lc := log.NewContext(logger).With("k", "v")
158 lc := log.With(logger, "k", "v")
178159 b.ReportAllocs()
179160 b.ResetTimer()
180161 for i := 0; i < b.N; i++ {
184165
185166 func BenchmarkTwoWith(b *testing.B) {
186167 logger := log.NewNopLogger()
187 lc := log.NewContext(logger).With("k", "v")
168 lc := log.With(logger, "k", "v")
188169 for i := 1; i < 2; i++ {
189 lc = lc.With("k", "v")
170 lc = log.With(lc, "k", "v")
190171 }
191172 b.ReportAllocs()
192173 b.ResetTimer()
197178
198179 func BenchmarkTenWith(b *testing.B) {
199180 logger := log.NewNopLogger()
200 lc := log.NewContext(logger).With("k", "v")
181 lc := log.With(logger, "k", "v")
201182 for i := 1; i < 10; i++ {
202 lc = lc.With("k", "v")
183 lc = log.With(lc, "k", "v")
203184 }
204185 b.ReportAllocs()
205186 b.ResetTimer()
1111 if err := logger.Log("abc", 123); err != nil {
1212 t.Error(err)
1313 }
14 if err := log.NewContext(logger).With("def", "ghi").Log(); err != nil {
14 if err := log.With(logger, "def", "ghi").Log(); err != nil {
1515 t.Error(err)
1616 }
1717 }
5555
5656 // copied from log/benchmark_test.go
5757 func benchmarkRunner(b *testing.B, logger log.Logger, f func(log.Logger)) {
58 lc := log.NewContext(logger).With("common_key", "common_value")
58 lc := log.With(logger, "common_key", "common_value")
5959 b.ReportAllocs()
6060 b.ResetTimer()
6161 for i := 0; i < b.N; i++ {
6565
6666 var (
6767 baseMessage = func(logger log.Logger) { logger.Log("foo_key", "foo_value") }
68 withMessage = func(logger log.Logger) { log.NewContext(logger).With("a", "b").Log("c", "d") }
68 withMessage = func(logger log.Logger) { log.With(logger, "a", "b").Log("c", "d") }
6969 )
7070
7171 // copied from log/concurrency_test.go
2323 return now
2424 }
2525
26 lc := log.NewContext(logger).With("ts", log.Timestamp(mocktime), "caller", log.DefaultCaller)
26 lc := log.With(logger, "ts", log.Timestamp(mocktime), "caller", log.DefaultCaller)
2727
2828 lc.Log("foo", "bar")
2929 timestamp, ok := output[1].(time.Time)
6767 return now
6868 }
6969
70 logger = log.NewContext(logger).With("ts", log.Timestamp(mocktime))
70 logger = log.With(logger, "ts", log.Timestamp(mocktime))
7171
7272 logger.Log()
7373 timestamp, ok := output[1].(time.Time)
9191
9292 func BenchmarkValueBindingTimestamp(b *testing.B) {
9393 logger := log.NewNopLogger()
94 lc := log.NewContext(logger).With("ts", log.DefaultTimestamp)
94 lc := log.With(logger, "ts", log.DefaultTimestamp)
9595 b.ReportAllocs()
9696 b.ResetTimer()
9797 for i := 0; i < b.N; i++ {
101101
102102 func BenchmarkValueBindingCaller(b *testing.B) {
103103 logger := log.NewNopLogger()
104 lc := log.NewContext(logger).With("caller", log.DefaultCaller)
104 lc := log.With(logger, "caller", log.DefaultCaller)
105105 b.ReportAllocs()
106106 b.ResetTimer()
107107 for i := 0; i < b.N; i++ {
4545 subscriber := NewSubscriber(
4646 client,
4747 factory,
48 log.NewContext(logger).With("component", "subscriber"),
48 log.With(logger, "component", "subscriber"),
4949 r.Name,
5050 r.Tags,
5151 true,
6363 }
6464
6565 // Build a registrar for r.
66 registrar := NewRegistrar(client, r, log.NewContext(logger).With("component", "registrar"))
66 registrar := NewRegistrar(client, r, log.With(logger, "component", "registrar"))
6767 registrar.Register()
6868 defer registrar.Deregister()
6969
2020 return &Registrar{
2121 client: client,
2222 registration: r,
23 logger: log.NewContext(logger).With("service", r.Name, "tags", fmt.Sprint(r.Tags), "address", r.Address),
23 logger: log.With(logger, "service", r.Name, "tags", fmt.Sprint(r.Tags), "address", r.Address),
2424 }
2525 }
2626
3535 s := &Subscriber{
3636 cache: cache.New(factory, logger),
3737 client: client,
38 logger: log.NewContext(logger).With("service", service, "tags", fmt.Sprint(tags)),
38 logger: log.With(logger, "service", service, "tags", fmt.Sprint(tags)),
3939 service: service,
4040 tags: tags,
4141 passingOnly: passingOnly,
4848 registrar := NewRegistrar(client, Service{
4949 Key: key,
5050 Value: value,
51 }, log.NewContext(log.NewLogfmtLogger(os.Stderr)).With("component", "registrar"))
51 }, log.With(log.NewLogfmtLogger(os.Stderr), "component", "registrar"))
5252
5353 // Register our instance.
5454 registrar.Register()
7070 client,
7171 prefix,
7272 func(string) (endpoint.Endpoint, io.Closer, error) { return endpoint.Nop, nil, nil },
73 log.NewContext(log.NewLogfmtLogger(os.Stderr)).With("component", "subscriber"),
73 log.With(log.NewLogfmtLogger(os.Stderr), "component", "subscriber"),
7474 )
7575 if err != nil {
7676 t.Fatalf("NewSubscriber: %v", err)
6262 return &Registrar{
6363 client: client,
6464 service: service,
65 logger: log.NewContext(logger).With(
66 "key", service.Key,
67 "value", service.Value,
68 ),
65 logger: log.With(logger, "key", service.Key, "value", service.Value),
6966 }
7067 }
7168
2323 return &Registrar{
2424 client: client,
2525 service: service,
26 logger: log.NewContext(logger).With(
26 logger: log.With(logger,
2727 "service", service.Name,
2828 "path", service.Path,
2929 "data", string(service.Data),