Created
June 3, 2018 21:11
-
-
Save antklim/918b2e4436a4ac03736d5657914b42c4 to your computer and use it in GitHub Desktop.
Go Kit greeter endpoints middleware
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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