759f7d9c4674aba24f416ad9509d84c7ed22df01.svn-base 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package booking
  2. import (
  3. "time"
  4. "github.com/go-kit/kit/log"
  5. shipping "github.com/longjoy/micro-go-course/section19/cargo/model"
  6. )
  7. type loggingService struct {
  8. logger log.Logger
  9. next Service
  10. }
  11. // NewLoggingService returns a new instance of a logging Service.
  12. func NewLoggingService(logger log.Logger, s Service) Service {
  13. return &loggingService{logger, s}
  14. }
  15. func (s *loggingService) BookNewCargo(origin shipping.UNLocode, destination shipping.UNLocode, deadline time.Time) (id shipping.TrackingID, err error) {
  16. defer func(begin time.Time) {
  17. s.logger.Log(
  18. "method", "book",
  19. "origin", origin,
  20. "destination", destination,
  21. "arrival_deadline", deadline,
  22. "took", time.Since(begin),
  23. "err", err,
  24. )
  25. }(time.Now())
  26. return s.next.BookNewCargo(origin, destination, deadline)
  27. }
  28. func (s *loggingService) LoadCargo(id shipping.TrackingID) (c Cargo, err error) {
  29. defer func(begin time.Time) {
  30. s.logger.Log(
  31. "method", "load",
  32. "tracking_id", id,
  33. "took", time.Since(begin),
  34. "err", err,
  35. )
  36. }(time.Now())
  37. return s.next.LoadCargo(id)
  38. }
  39. func (s *loggingService) RequestPossibleRoutesForCargo(id shipping.TrackingID) []shipping.Itinerary {
  40. defer func(begin time.Time) {
  41. s.logger.Log(
  42. "method", "request_routes",
  43. "tracking_id", id,
  44. "took", time.Since(begin),
  45. )
  46. }(time.Now())
  47. return s.next.RequestPossibleRoutesForCargo(id)
  48. }
  49. func (s *loggingService) AssignCargoToRoute(id shipping.TrackingID, itinerary shipping.Itinerary) (res bool, err error) {
  50. defer func(begin time.Time) {
  51. s.logger.Log(
  52. "method", "assign_to_route",
  53. "tracking_id", id,
  54. "took", time.Since(begin),
  55. "err", err,
  56. )
  57. }(time.Now())
  58. return s.next.AssignCargoToRoute(id, itinerary)
  59. }
  60. func (s *loggingService) ChangeDestination(id shipping.TrackingID, l shipping.UNLocode) (res bool, err error) {
  61. defer func(begin time.Time) {
  62. s.logger.Log(
  63. "method", "change_destination",
  64. "tracking_id", id,
  65. "destination", l,
  66. "took", time.Since(begin),
  67. "err", err,
  68. )
  69. }(time.Now())
  70. return s.next.ChangeDestination(id, l)
  71. }
  72. func (s *loggingService) Cargos() []Cargo {
  73. defer func(begin time.Time) {
  74. s.logger.Log(
  75. "method", "list_cargos",
  76. "took", time.Since(begin),
  77. )
  78. }(time.Now())
  79. return s.next.Cargos()
  80. }
  81. func (s *loggingService) Locations() []Location {
  82. defer func(begin time.Time) {
  83. s.logger.Log(
  84. "method", "list_locations",
  85. "took", time.Since(begin),
  86. )
  87. }(time.Now())
  88. return s.next.Locations()
  89. }