-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.go
60 lines (49 loc) · 1.09 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
package router
import (
"time"
"go.uber.org/zap"
)
func LoggingMiddleware(log *zap.Logger) ServiceMiddleware {
return func(next Service) Service {
return &loggingMiddleware{
log: log.With(zap.String("service", "router")),
next: next,
}
}
}
type loggingMiddleware struct {
log *zap.Logger
next Service
}
func (mw *loggingMiddleware) Close() {
mw.next.Close()
}
func (mw *loggingMiddleware) Replay(topic string, since time.Time, from string, to string) (string, error) {
log := mw.log.With(
zap.String("action", "replay"),
zap.String("from", from),
zap.String("to", to),
zap.String("topic", topic),
zap.Time("since", since),
)
id, err := mw.next.Replay(topic, since, from, to)
if err != nil {
log.Error(err.Error())
return "", err
}
log.Info("replay created", zap.String("id", id))
return id, nil
}
func (mw *loggingMiddleware) CloseTask(id string) error {
log := mw.log.With(
zap.String("action", "close_task"),
zap.String("id", id),
)
err := mw.next.CloseTask(id)
if err != nil {
log.Error(err.Error())
return err
}
log.Info("task closed")
return nil
}