-
Notifications
You must be signed in to change notification settings - Fork 0
/
httplog.go
233 lines (216 loc) · 6.37 KB
/
httplog.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
// Package httplog provides logging for http requests.
//
// Apart from a ready to use logger that can be used freely, the package also
// provides a logging middleware (or wrapper) over http Handlers.
//
// The logger outputs a small set of default parameters and provides an
// extensible way to log extra parameters if needed. The log format is a nice
// balance between human and machine readability.
//
// The log output is one line per request. The parameters are separated with a
// blank space while the parameter key and its value are separated by the "="
// character. Here's an example log output for a single request.
//
// level=I time=2017-07-08T17:08:12UTC ip=193.92.20.19 method=GET path=/logs ua=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36 status=200 params={}
//
// Standalone usage
//
// The logger needs a stream that implements the io.Writer interface. This is where
// all logging output will go.
//
// type stream struct {}
//
// func (s *stream) Write(p []byte) (n int, err error) {
// // write somewhere
// }
//
// l := httplog.New(&stream{})
// l.Log()
//
// Middleware usage
//
// You just need to provide your handler and a logger as arguments to the
// httplog.WithLogging function.
//
// type customHandler {}
//
// func (h *customHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// w.WriteHeader(200)
// // ...your handler logic goes here...
// }
//
// func main() {
// // Configure a logger
// l := httplog.New(os.Stdout)
//
// // And use the middleware
// http.Handle("/logs", httplog.WithLogging(&customHandler{}, l))
// http.ListenAndServe(":8080", nil)
// }
//
// Adding extra log parameters
//
// type User struct {
// ID int
// }
// user := &User{ID: 1234}
//
// l := httplog.New(os.Stdout)
// l.Add("uid", user.ID)
// l.Add("meta", "new-request")
// l.Log()
// // => level=I [...] uid=1234 meta=new-request
//
package httplog
import (
"bufio"
"fmt"
"io"
"net"
"net/http"
"net/http/httputil"
"regexp"
"strconv"
"strings"
"time"
)
// Logger is the interface that wraps the basic Log method.
// Log builds a log entry with all the parameters the Logger has been
// configured with and writes the log entry to the underlying
// io.Writer's stream.
//
// The Logger can be configured with information from the request, using
// the SetRequestInfo method. After the request is made, the SetStatus
// method will set the response status.
//
// Logger also provides an Add method, to add extra parameters for
// logging.
type Logger interface {
Log()
SetStatus(int)
SetRequestInfo(*http.Request)
Add(string, interface{})
}
// Concrete implementation of the Logger interface
type httpLogger struct {
w io.Writer
ip string
method string
path string
ua string
params string
status int
reqRaw []byte
extras map[string]interface{}
}
// New returns a Logger configured with the supplied io.Writer.
func New(w io.Writer) Logger {
return &httpLogger{
w: w,
extras: make(map[string]interface{}, 0),
}
}
// Log produces the logging entry for a single request.
// It appends a logging line to the io.Writer's stream, using the io.Writer's
// Write function. The entry is terminated by the new line character.
func (l *httpLogger) Log() {
l.w.Write(append(l.buildLogEntry(), '\n'))
}
// Add adds an extra logging parameter that will be included in the log output.
// It needs the name of the parameter and its value. The output will be
// <name>=<value>
func (l *httpLogger) Add(key string, value interface{}) {
l.extras[key] = value
}
// SetStatus sets the Logger's status field to the supplied value.
func (l *httpLogger) SetStatus(s int) {
l.status = s
}
// SetRequestInfo sets all Logger fields that can be extracted from the
// supplied http.Request argument. These are:
// - the request IP
// - the request method
// - the user agent header value
// - the path
// - the request parameters, either from the request body or from the query URL
func (l *httpLogger) SetRequestInfo(r *http.Request) {
l.ip = getIP(r)
// Get a request dump
l.reqRaw = reqDump(r)
var line string
pathRegexp, _ := regexp.Compile("(.+)\\s(.+)\\sHTTP")
userAgentRegexp, _ := regexp.Compile("User-Agent:\\s(.+)")
getParamsRegexp, _ := regexp.Compile("(.+)\\?(.+)")
// The raw request comes in lines, separated by \r\n
s := bufio.NewScanner(strings.NewReader(string(l.reqRaw)))
for s.Scan() {
line = s.Text()
l.setPath(line, pathRegexp, getParamsRegexp)
l.setUa(line, userAgentRegexp)
}
// Last line contains the request parameters
if len(l.params) == 0 {
l.params = line
}
}
func (l *httpLogger) buildLogEntry() []byte {
buf := make([]byte, 0)
buf = append(buf, "level=I"...)
buf = append(buf, " time="+time.Now().UTC().Format("2006-01-02T15:04:05MST")...)
buf = append(buf, " ip="+l.ip...)
buf = append(buf, " method="+l.method...)
buf = append(buf, " path="+l.path...)
buf = append(buf, " ua="+l.ua...)
buf = append(buf, " status="+strconv.Itoa(l.status)...)
buf = append(buf, " params="+l.params...)
for k, v := range l.extras {
buf = append(buf, " "+k+"="+fmt.Sprintf("%v", v)...)
}
return buf
}
func (l *httpLogger) setPath(path string, pathRegexp *regexp.Regexp, getParamsRegexp *regexp.Regexp) {
// Check for the request path portion
// example POST /path HTTP/1.1
matches := pathRegexp.FindStringSubmatch(path)
if len(matches) > 0 {
l.method = matches[1]
l.path = matches[2]
// Check for query string params (GET request)
// example GET /path?param1=value¶m2=value
matches = getParamsRegexp.FindStringSubmatch(matches[2])
if len(matches) > 0 {
l.path = matches[1]
l.params = toJSON(matches[2])
}
}
}
func (l *httpLogger) setUa(h string, r *regexp.Regexp) {
// Check for user agent header
// example User-Agent: <ua>
if matches := r.FindStringSubmatch(h); len(matches) > 0 {
l.ua = matches[1]
}
}
func getIP(r *http.Request) (ip string) {
if forwarded := r.Header.Get("X-Forwarded-For"); len(forwarded) > 0 {
ip = forwarded
return
}
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
ip = r.RemoteAddr
}
return
}
func reqDump(r *http.Request) (dump []byte) {
dump, err := httputil.DumpRequest(r, true)
if err != nil {
dump = []byte("")
}
return
}
// Poor man's JSON encoding
func toJSON(s string) string {
r := strings.NewReplacer("=", "\": \"", "&", "\", \"")
return "{\"" + r.Replace(s) + "\"}"
}