-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaccesslog.go
244 lines (210 loc) · 5.9 KB
/
accesslog.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package accesslog
import (
"bufio"
"context"
"fmt"
"net"
"net/http"
"strings"
"time"
)
type contextKey int
var (
ctxLoggerKey contextKey
)
type LogRecord struct {
Time time.Time
Ip, Method, Uri, Protocol, Username, Host string
Status int
Size int64
ElapsedTime time.Duration
RequestHeader http.Header
CustomRecords map[string]string
}
type LoggingWriter struct {
http.ResponseWriter
logRecord LogRecord
}
func (r *LoggingWriter) Write(p []byte) (int, error) {
if r.logRecord.Status == 0 {
// The status will be StatusOK if WriteHeader has not been called yet
r.logRecord.Status = http.StatusOK
}
written, err := r.ResponseWriter.Write(p)
r.logRecord.Size += int64(written)
return written, err
}
func (r *LoggingWriter) WriteHeader(status int) {
r.logRecord.Status = status
r.ResponseWriter.WriteHeader(status)
}
// SetCustomLogRecord and GetCustomLogRecord functions provide accessors to the logRecord.CustomRecords.
// You can use it to store arbitrary strings that are relevant to this request.
//
// Alternative method would be to store the value in context.
// Which doesn't work when you want to retrieve the value from a HTTP middleware that is earlier in the middleware chain, eg: accesslog, recovery.
//
// w.(accesslogger.LoggingWriter).SetCustomLogRecord("X-User-Id", "3")
func (r *LoggingWriter) SetCustomLogRecord(key, value string) {
if r.logRecord.CustomRecords == nil {
r.logRecord.CustomRecords = map[string]string{}
}
r.logRecord.CustomRecords[key] = value
}
// w.(accesslogger.LoggingWriter).GetCustomLogRecord("X-User-Id")
func (r *LoggingWriter) GetCustomLogRecord(key string) string{
return r.logRecord.CustomRecords[key]
}
// http.CloseNotifier interface
func (r *LoggingWriter) CloseNotify() <-chan bool {
if w, ok := r.ResponseWriter.(http.CloseNotifier); ok {
return w.CloseNotify()
}
return make(chan bool)
}
func (r *LoggingWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if hijacker, ok := r.ResponseWriter.(http.Hijacker); ok {
return hijacker.Hijack()
}
return nil, nil, fmt.Errorf("ResponseWriter doesn't support Hijacker interface")
}
// http.Flusher
func (r *LoggingWriter) Flush() {
flusher, ok := r.ResponseWriter.(http.Flusher)
if ok {
flusher.Flush()
}
}
// http.Pusher
func (r *LoggingWriter) Push(target string, opts *http.PushOptions) error {
pusher, ok := r.ResponseWriter.(http.Pusher)
if ok {
return pusher.Push(target, opts)
}
return fmt.Errorf("ResponseWriter doesn't support Pusher interface")
}
// WrapWriter interface
func (r *LoggingWriter) WrappedWriter() http.ResponseWriter {
return r.ResponseWriter
}
type Logger interface {
Log(record LogRecord)
}
type ContextLogger interface {
Logger
LogContext(context.Context, LogRecord)
}
type LoggingHandler struct {
handler http.Handler
logger ContextLogger
logBefore bool
}
type wrapLogger struct {
Logger
}
func (wl *wrapLogger) LogContext(ctx context.Context, l LogRecord) {
wl.Log(l)
}
func contextLogger(l Logger) ContextLogger {
if cl, ok := l.(ContextLogger); ok {
return cl
}
return &wrapLogger{l}
}
func NewLoggingHandler(handler http.Handler, logger Logger) http.Handler {
return &LoggingHandler{
handler: handler,
logger: contextLogger(logger),
logBefore: false,
}
}
func NewAroundLoggingHandler(handler http.Handler, logger Logger) http.Handler {
return &LoggingHandler{
handler: handler,
logger: contextLogger(logger),
logBefore: true,
}
}
func NewLoggingMiddleware(logger Logger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
handler := NewLoggingHandler(next, logger)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handler.ServeHTTP(w, r)
})
}
}
func NewAroundLoggingMiddleware(logger Logger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
handler := NewAroundLoggingHandler(next, logger)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handler.ServeHTTP(w, r)
})
}
}
// readIp return the real ip when behide nginx or apache
func (h *LoggingHandler) realIp(r *http.Request) string {
// Check if behide nginx or apache
xRealIP := r.Header.Get("X-Real-Ip")
if xRealIP != "" {
return xRealIP
}
xForwardedFor := r.Header.Get("X-Forwarded-For")
for _, address := range strings.Split(xForwardedFor, ",") {
address = strings.TrimSpace(address)
if address != "" {
return address
}
}
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return r.RemoteAddr
}
return ip
}
func GetLoggingWriter(ctx context.Context) *LoggingWriter {
iface := ctx.Value(ctxLoggerKey)
if l, ok := iface.(*LoggingWriter); ok {
return l
}
return nil
}
func (h *LoggingHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
ip := h.realIp(r)
username := "-"
if r.URL.User != nil {
if name := r.URL.User.Username(); name != "" {
username = name
}
}
startTime := time.Now()
writer := &LoggingWriter{
ResponseWriter: rw,
logRecord: LogRecord{
Time: startTime.UTC(),
Ip: ip,
Method: r.Method,
Uri: r.RequestURI,
Username: username,
Protocol: r.Proto,
Host: r.Host,
Status: 0,
Size: 0,
ElapsedTime: time.Duration(0),
RequestHeader: r.Header,
},
}
if h.logBefore {
writer.SetCustomLogRecord("at", "before")
h.logger.LogContext(r.Context(), writer.logRecord)
}
ctx := context.WithValue(r.Context(), ctxLoggerKey, writer)
r = r.WithContext(ctx)
h.handler.ServeHTTP(writer, r)
finishTime := time.Now()
writer.logRecord.Time = finishTime.UTC()
writer.logRecord.ElapsedTime = finishTime.Sub(startTime)
if h.logBefore {
writer.SetCustomLogRecord("at", "after")
}
h.logger.LogContext(r.Context(), writer.logRecord)
}