Skip to content

Instantly share code, notes, and snippets.

@antklim
Created June 3, 2018 21:11
Show Gist options
  • Select an option

  • Save antklim/918b2e4436a4ac03736d5657914b42c4 to your computer and use it in GitHub Desktop.

Select an option

Save antklim/918b2e4436a4ac03736d5657914b42c4 to your computer and use it in GitHub Desktop.
Go Kit greeter endpoints middleware
package greeterendpoint
import (
"context"
"time"
"github.com/go-kit/kit/endpoint"
"github.com/go-kit/kit/log"
)
// LoggingMiddleware returns an endpoint middleware that logs the
// duration of each invocation, and the resulting error, if any.
func LoggingMiddleware(logger log.Logger) endpoint.Middleware {
return func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
defer func(begin time.Time) {
logger.Log("transport_error", err, "took", time.Since(begin))
}(time.Now())
return next(ctx, request)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment