logging.go 730 B

1234567891011121314151617181920212223242526272829303132333435
  1. package user
  2. import (
  3. "context"
  4. "github.com/go-kit/kit/log"
  5. "time"
  6. )
  7. type ServiceMiddleware func(service UserService) UserService
  8. type loggingMiddleware struct {
  9. UserService
  10. logger log.Logger
  11. }
  12. func LoggingMiddleware(logger log.Logger) ServiceMiddleware {
  13. return func(next UserService) UserService {
  14. return loggingMiddleware{next, logger}
  15. }
  16. }
  17. func (mw loggingMiddleware) CheckPassword(ctx context.Context, username, password string) (ret bool, err error) {
  18. defer func(begin time.Time) {
  19. mw.logger.Log(
  20. "function", "CheckPassword",
  21. "username", username,
  22. "result", ret,
  23. "took", time.Since(begin),
  24. )
  25. }(time.Now())
  26. ret, err = mw.UserService.CheckPassword(ctx, username, password)
  27. return ret, err
  28. }