-
Notifications
You must be signed in to change notification settings - Fork 70
/
log.go
102 lines (91 loc) · 2.84 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
96
97
98
99
100
101
102
package courier
import (
"log/slog"
"net/http"
"time"
)
// LogMsgStatusReceived logs our that we received a new MsgStatus
func LogMsgStatusReceived(r *http.Request, status StatusUpdate) {
if slog.Default().Enabled(r.Context(), slog.LevelDebug) {
slog.Debug("status updated",
"channel_uuid", status.ChannelUUID(),
"url", r.Context().Value(contextRequestURL),
"elapsed_ms", getElapsedMS(r),
"status", status.Status(),
"msg_id", status.MsgID(),
"msg_external_id", status.ExternalID(),
)
}
}
// LogMsgReceived logs that we received the passed in message
func LogMsgReceived(r *http.Request, msg MsgIn) {
if slog.Default().Enabled(r.Context(), slog.LevelDebug) {
slog.Debug("msg received",
"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(),
)
}
}
// LogChannelEventReceived logs that we received the passed in channel event
func LogChannelEventReceived(r *http.Request, event ChannelEvent) {
if slog.Default().Enabled(r.Context(), slog.LevelDebug) {
slog.Debug("event received",
"channel_uuid", event.ChannelUUID(),
"url", r.Context().Value(contextRequestURL),
"elapsed_ms", getElapsedMS(r),
"event_type", event.EventType(),
"event_urn", event.URN().Identity(),
)
}
}
// LogRequestIgnored logs that we ignored the passed in request
func LogRequestIgnored(r *http.Request, channel Channel, details string) {
if slog.Default().Enabled(r.Context(), slog.LevelDebug) {
slog.Debug("request ignored",
"channel_uuid", channel.UUID(),
"url", r.Context().Value(contextRequestURL),
"elapsed_ms", getElapsedMS(r),
"details", details,
)
}
}
// LogRequestHandled logs that we handled the passed in request but didn't create any events
func LogRequestHandled(r *http.Request, channel Channel, details string) {
if slog.Default().Enabled(r.Context(), slog.LevelDebug) {
slog.Debug("request handled",
"channel_uuid", channel.UUID(),
"url", r.Context().Value(contextRequestURL),
"elapsed_ms", getElapsedMS(r),
"details", details,
)
}
}
// 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 := slog.With(
"url", r.Context().Value(contextRequestURL),
"elapsed_ms", getElapsedMS(r),
"error", err,
)
if channel != nil {
log = log.With("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.Since(startTime)) / float64(time.Millisecond)
}