-
Notifications
You must be signed in to change notification settings - Fork 61
/
tracker.go
38 lines (30 loc) · 983 Bytes
/
tracker.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
package data
import (
"time"
"github.com/PacktPublishing/Hands-On-Dependency-Injection-in-Go/ch12/acme/internal/logging"
)
// QueryTracker is an interface to track query timing
type QueryTracker interface {
// Track will record/out the time a query took by calculating time.Now().Sub(start)
Track(key string, start time.Time)
}
// NO-OP implementation of QueryTracker
type noopTracker struct{}
// Track implements QueryTracker
func (_ *noopTracker) Track(_ string, _ time.Time) {
// intentionally does nothing
}
// NewLogTracker returns a Tracker that outputs tracking data to log
func NewLogTracker(logger logging.Logger) *LogTracker {
return &LogTracker{
logger: logger,
}
}
// LogTracker implements QueryTracker and outputs to the supplied logger
type LogTracker struct {
logger logging.Logger
}
// Track implements QueryTracker
func (l *LogTracker) Track(key string, start time.Time) {
l.logger.Info("[%s] Timing: %s\n", key, time.Now().Sub(start).String())
}