forked from nyaruka/courier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel_log.go
98 lines (84 loc) · 2.7 KB
/
channel_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
96
97
98
package courier
import (
"fmt"
"time"
"github.com/nyaruka/courier/utils"
)
// NilStatusCode is used when we have an error before even sending anything
const NilStatusCode int = 417
// NewChannelLog creates a new channel log for the passed in channel, id, and request and response info
func NewChannelLog(description string, channel Channel, msgID MsgID, method string, url string, statusCode int,
request string, response string, elapsed time.Duration, err error) *ChannelLog {
errString := ""
if err != nil {
errString = err.Error()
}
return &ChannelLog{
Description: description,
Channel: channel,
MsgID: msgID,
Method: method,
URL: url,
StatusCode: statusCode,
Error: errString,
Request: request,
Response: response,
CreatedOn: time.Now(),
Elapsed: elapsed,
}
}
// NewChannelLogFromRR creates a new channel log for the passed in channel, id, and request/response log
func NewChannelLogFromRR(description string, channel Channel, msgID MsgID, rr *utils.RequestResponse) *ChannelLog {
log := &ChannelLog{
Description: description,
Channel: channel,
MsgID: msgID,
Method: rr.Method,
URL: rr.URL,
StatusCode: rr.StatusCode,
Request: rr.Request,
Response: rr.Response,
CreatedOn: time.Now(),
Elapsed: rr.Elapsed,
}
return log
}
// NewChannelLogFromError creates a new channel log for the passed in channel, msg id and error
func NewChannelLogFromError(description string, channel Channel, msgID MsgID, elapsed time.Duration, err error) *ChannelLog {
log := &ChannelLog{
Description: description,
Channel: channel,
MsgID: msgID,
Error: err.Error(),
CreatedOn: time.Now(),
Elapsed: elapsed,
}
return log
}
// WithError augments the passed in ChannelLog with the passed in description and error if error is not nil
func (l *ChannelLog) WithError(description string, err error) *ChannelLog {
if err != nil {
l.Error = err.Error()
l.Description = description
}
return l
}
func (l *ChannelLog) String() string {
return fmt.Sprintf("%s: %d %s %d\n%s\n%s\n%s", l.Description, l.StatusCode, l.URL, l.Elapsed, l.Error, l.Request, l.Response)
}
// ChannelLog represents the log for a msg being received, sent or having its status updated. It includes the HTTP request
// and response for the action as well as the channel it was performed on and an option ID of the msg (for some error
// cases we may log without a msg id)
type ChannelLog struct {
Description string
Channel Channel
MsgID MsgID
Method string
URL string
StatusCode int
Error string
Request string
Response string
Elapsed time.Duration
CreatedOn time.Time
}