-
Notifications
You must be signed in to change notification settings - Fork 3
/
events.go
63 lines (55 loc) · 2.69 KB
/
events.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
package request
import (
"fmt"
"strconv"
logger "github.com/blendlabs/go-logger"
)
const (
// Event is a diagnostics agent event flag.
Event logger.EventFlag = "request"
// EventResponse is a diagnostics agent event flag.
EventResponse logger.EventFlag = "request.response"
)
// NewOutgoingListener creates a new logger handler for `EventFlagOutgoingResponse` events.
func NewOutgoingListener(handler func(writer logger.Logger, ts logger.TimeSource, req *Meta)) logger.EventListener {
return func(writer logger.Logger, ts logger.TimeSource, eventFlag logger.EventFlag, state ...interface{}) {
handler(writer, ts, state[0].(*Meta))
}
}
// WriteOutgoingRequest is a helper method to write outgoing request events to a logger writer.
func WriteOutgoingRequest(writer logger.Logger, ts logger.TimeSource, req *Meta) {
buffer := writer.GetBuffer()
defer writer.PutBuffer(buffer)
buffer.WriteString(writer.Colorize(string(Event), logger.ColorGreen))
buffer.WriteRune(logger.RuneSpace)
buffer.WriteString(fmt.Sprintf("%s %s", req.Verb, req.URL.String()))
writer.WriteWithTimeSource(ts, buffer.Bytes())
}
// WriteOutgoingRequestBody is a helper method to write outgoing request bodies to a logger writer.
func WriteOutgoingRequestBody(writer logger.Logger, ts logger.TimeSource, req *Meta) {
buffer := writer.GetBuffer()
defer writer.PutBuffer(buffer)
buffer.WriteString(writer.Colorize(string(Event), logger.ColorGreen))
buffer.WriteRune(logger.RuneSpace)
buffer.WriteString("request body")
buffer.WriteRune(logger.RuneNewline)
buffer.Write(req.Body)
writer.WriteWithTimeSource(ts, buffer.Bytes())
}
// NewOutgoingResponseListener creates a new logger handler for `EventFlagOutgoingResponse` events.
func NewOutgoingResponseListener(handler func(writer logger.Logger, ts logger.TimeSource, req *Meta, res *ResponseMeta, body []byte)) logger.EventListener {
return func(writer logger.Logger, ts logger.TimeSource, eventFlag logger.EventFlag, state ...interface{}) {
handler(writer, ts, state[0].(*Meta), state[1].(*ResponseMeta), state[2].([]byte))
}
}
// WriteOutgoingRequestResponse is a helper method to write outgoing request response events to a logger writer.
func WriteOutgoingRequestResponse(writer logger.Logger, ts logger.TimeSource, req *Meta, res *ResponseMeta, body []byte) {
buffer := writer.GetBuffer()
defer writer.PutBuffer(buffer)
buffer.WriteString(writer.Colorize(string(EventResponse), logger.ColorGreen))
buffer.WriteRune(logger.RuneSpace)
buffer.WriteString(fmt.Sprintf("%s %s %s", writer.ColorizeByStatusCode(res.StatusCode, strconv.Itoa(res.StatusCode)), req.Verb, req.URL.String()))
buffer.WriteRune(logger.RuneNewline)
buffer.Write(body)
writer.WriteWithTimeSource(ts, buffer.Bytes())
}