/
log.go
149 lines (117 loc) · 4.44 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package wrapper
import (
"encoding/json"
"os"
"time"
"github.com/rs/zerolog"
)
// init is called at the start of the application.
func init() {
zerolog.TimeFieldFormat = time.RFC3339Nano
zerolog.SetGlobalLevel(zerolog.Disabled)
}
var (
// Logger represents the Disgo Logger used to log information.
Logger = zerolog.New(os.Stdout)
)
// Logger Contexts
const (
// LogCtxClient represents the log key for a Client (Bot) Application ID.
LogCtxClient = "client"
// LogCtxCorrelation represents the log key for a Correlation ID.
LogCtxCorrelation = "xid"
// LogCtxRequest represents the log key for a Request ID.
LogCtxRequest = "request"
// LogCtxRoute represents the log key for a Route ID.
LogCtxRoute = "route"
// LogCtxResource represents the log key for a Resource ID.
LogCtxResource = "resource"
// LogCtxEndpoint represents the log key for an Endpoint.
LogCtxEndpoint = "endpoint"
// LogCtxRequestBody represents the log key for an HTTP Request Body.
LogCtxRequestBody = "body"
// LogCtxBucket represents the log key for a Rate Limit Bucket ID.
LogCtxBucket = "bucket"
// LogCtxReset represents the log key for a Discord Bucket reset time.
LogCtxReset = "reset"
// LogCtxResponse represents the log key for an HTTP Request Response.
LogCtxResponse = "response"
// LogCtxResponseHeader represents the log key for an HTTP Request Response header.
LogCtxResponseHeader = "header"
// LogCtxResponseBody represents the log key for an HTTP Request Response body.
LogCtxResponseBody = "body"
// LogCtxRequestRateLimitCode represents the log key for an HTTP Rate Limit Response code.
LogCtxRequestRateLimitCode = "code"
// LogCtxSession represents the log key for a Discord Session ID.
LogCtxSession = "session"
// LogCtxPayload represents the log key for a Discord Gateway Payload.
LogCtxPayload = "payload"
// LogCtxPayloadOpcode represents the log key for a Discord Gateway Payload opcode.
LogCtxPayloadOpcode = "opcode"
// LogCtxPayloadData represents the log key for Discord Gateway Payload data.
LogCtxPayloadData = "data"
// LogCtxEvent represents the log key for a Discord Gateway Event.
LogCtxEvent = "event"
// LogCtxCommand represents the log key for a Discord Gateway command.
LogCtxCommand = "command"
// LogCtxCommandOpcode represents the log key for a Discord Gateway command opcode.
LogCtxCommandOpcode = "opcode"
// LogCtxCommandName represents the log key for a Discord Gateway command name.
LogCtxCommandName = "name"
)
// LogRequest logs a request.
func LogRequest(log *zerolog.Event, clientid, xid, routeid, resourceid, endpoint string) *zerolog.Event {
return log.Timestamp().
Str(LogCtxClient, clientid).
Dict(LogCtxRequest, zerolog.Dict().
Str(LogCtxCorrelation, xid).
Str(LogCtxRoute, routeid).
Str(LogCtxResource, resourceid).
Str(LogCtxEndpoint, endpoint),
)
}
// LogRequestBody logs a request with its body.
func LogRequestBody(log *zerolog.Event, clientid, xid, routeid, resourceid, endpoint, body string) *zerolog.Event {
return log.Timestamp().
Str(LogCtxClient, clientid).
Dict(LogCtxRequest, zerolog.Dict().
Str(LogCtxCorrelation, xid).
Str(LogCtxRoute, routeid).
Str(LogCtxResource, resourceid).
Str(LogCtxEndpoint, endpoint).
Str(LogCtxRequestBody, body),
)
}
// LogResponse logs a response (typically using LogRequest).
func LogResponse(log *zerolog.Event, header, body string) *zerolog.Event {
return log.Dict(LogCtxResponse, zerolog.Dict().
Str(LogCtxResponseHeader, header).
Str(LogCtxResponseBody, body),
)
}
// LogEventHandler logs an event handler action.
func LogEventHandler(log *zerolog.Event, clientid, event string) *zerolog.Event {
return log.Timestamp().
Str(LogCtxClient, clientid).
Str(LogCtxEvent, event)
}
// LogSession logs a session.
func LogSession(log *zerolog.Event, sessionid string) *zerolog.Event {
return log.Timestamp().
Str(LogCtxSession, sessionid)
}
// LogPayload logs a Discord Gateway Payload (typically using LogSession).
func LogPayload(log *zerolog.Event, op int, data json.RawMessage) *zerolog.Event {
return log.Dict(LogCtxPayload, zerolog.Dict().
Int(LogCtxPayloadOpcode, op).
Bytes(LogCtxPayloadData, data),
)
}
// LogCommand logs a Gateway Command (typically using a LogSession).
func LogCommand(log *zerolog.Event, clientid string, op int, command string) *zerolog.Event {
return log.Str(LogCtxClient, clientid).
Dict(LogCtxCommand, zerolog.Dict().
Int(LogCtxCommandOpcode, op).
Str(LogCtxCommandName, command),
)
}