forked from nyaruka/courier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
95 lines (85 loc) · 2.9 KB
/
log.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
package courier
import (
"net/http"
"time"
"github.com/sirupsen/logrus"
)
// LogMsgStatusReceived logs our that we received a new MsgStatus
func LogMsgStatusReceived(r *http.Request, status MsgStatus) {
log := logrus.WithFields(logrus.Fields{
"channel_uuid": status.ChannelUUID(),
"url": r.Context().Value(contextRequestURL),
"elapsed_ms": getElapsedMS(r),
"status": status.Status(),
})
if status.ID() != NilMsgID {
log = log.WithField("msg_id", status.ID())
} else {
log = log.WithField("msg_external_id", status.ExternalID())
}
log.Info("status updated")
}
// LogMsgReceived logs that we received the passed in message
func LogMsgReceived(r *http.Request, msg Msg) {
logrus.WithFields(logrus.Fields{
"channel_uuid": msg.Channel().UUID(),
"url": r.Context().Value(contextRequestURL),
"elapsed_ms": getElapsedMS(r),
"msg_uuid": msg.UUID(),
"msg_id": msg.ID(),
"msg_urn": msg.URN().Identity(),
"msg_text": msg.Text(),
"msg_attachments": msg.Attachments(),
}).Info("msg received")
}
// LogChannelEventReceived logs that we received the passed in channel event
func LogChannelEventReceived(r *http.Request, event ChannelEvent) {
logrus.WithFields(logrus.Fields{
"channel_uuid": event.ChannelUUID(),
"url": r.Context().Value(contextRequestURL),
"elapsed_ms": getElapsedMS(r),
"event_type": event.EventType(),
"event_urn": event.URN().Identity(),
}).Info("evt received")
}
// LogRequestIgnored logs that we ignored the passed in request
func LogRequestIgnored(r *http.Request, channel Channel, details string) {
logrus.WithFields(logrus.Fields{
"channel_uuid": channel.UUID(),
"url": r.Context().Value(contextRequestURL),
"elapsed_ms": getElapsedMS(r),
"details": details,
}).Info("request ignored")
}
// LogRequestHandled logs that we handled the passed in request but didn't create any events
func LogRequestHandled(r *http.Request, channel Channel, details string) {
logrus.WithFields(logrus.Fields{
"channel_uuid": channel.UUID(),
"url": r.Context().Value(contextRequestURL),
"elapsed_ms": getElapsedMS(r),
"details": details,
}).Info("request handled")
}
// LogRequestError logs that errored during parsing (this is logged as an info as it isn't an error on our side)
func LogRequestError(r *http.Request, channel Channel, err error) {
log := logrus.WithFields(logrus.Fields{
"url": r.Context().Value(contextRequestURL),
"elapsed_ms": getElapsedMS(r),
"error": err.Error(),
})
if channel != nil {
log = log.WithField("channel_uuid", channel.UUID())
}
log.Info("request errored")
}
func getElapsedMS(r *http.Request) float64 {
start := r.Context().Value(contextRequestStart)
if start == nil {
return -1
}
startTime, isTime := start.(time.Time)
if !isTime {
return -1
}
return float64(time.Now().Sub(startTime)) / float64(time.Millisecond)
}