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