forked from nyaruka/courier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
70 lines (61 loc) · 1.89 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
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(channel Channel, msgID MsgID, method string, url string, statusCode int, err error,
request string, response string, elapsed time.Duration, createdOn time.Time) *ChannelLog {
errString := ""
if err != nil {
errString = err.Error()
}
return &ChannelLog{
Channel: channel,
MsgID: msgID,
Method: method,
URL: url,
StatusCode: statusCode,
Error: errString,
Request: request,
Response: response,
CreatedOn: createdOn,
Elapsed: elapsed,
}
}
// NewChannelLogFromRR creates a new channel log for the passed in channel, id, and request/response log
func NewChannelLogFromRR(channel Channel, msgID MsgID, rr *utils.RequestResponse) *ChannelLog {
return &ChannelLog{
Channel: channel,
MsgID: msgID,
Method: rr.Method,
URL: rr.URL,
StatusCode: rr.StatusCode,
Error: "",
Request: rr.Request,
Response: rr.Response,
CreatedOn: time.Now(),
Elapsed: rr.Elapsed,
}
}
func (l *ChannelLog) String() string {
return fmt.Sprintf("%d %s %d\n%s\n%s\n%s", 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 {
Channel Channel
MsgID MsgID
Method string
URL string
StatusCode int
Error string
Request string
Response string
Elapsed time.Duration
CreatedOn time.Time
}