6f2e66831bdc0e1782077cebc7f117dd820c7228.svn-base 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package main
  2. import (
  3. "context"
  4. "github.com/go-kit/kit/log"
  5. "github.com/longjoy/micro-go-course/section35/zipkin-kit/string-service/service"
  6. "time"
  7. )
  8. // loggingMiddleware Make a new type
  9. // that contains Service interface and logger instance
  10. type loggingMiddleware struct {
  11. service.Service
  12. logger log.Logger
  13. }
  14. // LoggingMiddleware make logging middleware
  15. func LoggingMiddleware(logger log.Logger) service.ServiceMiddleware {
  16. return func(next service.Service) service.Service {
  17. return loggingMiddleware{next, logger}
  18. }
  19. }
  20. func (mw loggingMiddleware) Concat(a, b string) (ret string, err error) {
  21. defer func(begin time.Time) {
  22. mw.logger.Log(
  23. "function", "Concat",
  24. "a", a,
  25. "b", b,
  26. "result", ret,
  27. "took", time.Since(begin),
  28. )
  29. }(time.Now())
  30. ret, err = mw.Service.Concat(a, b)
  31. return ret, err
  32. }
  33. func (mw loggingMiddleware) Diff(ctx context.Context, a, b string) (ret string, err error) {
  34. defer func(begin time.Time) {
  35. mw.logger.Log(
  36. "function", "Diff",
  37. "a", a,
  38. "b", b,
  39. "result", ret,
  40. "took", time.Since(begin),
  41. )
  42. }(time.Now())
  43. ret, err = mw.Service.Diff(ctx, a, b)
  44. return ret, err
  45. }
  46. func (mw loggingMiddleware) HealthCheck() (result bool) {
  47. defer func(begin time.Time) {
  48. mw.logger.Log(
  49. "function", "HealthChcek",
  50. "result", result,
  51. "took", time.Since(begin),
  52. )
  53. }(time.Now())
  54. result = mw.Service.HealthCheck()
  55. return
  56. }