-
Notifications
You must be signed in to change notification settings - Fork 2
/
http_log.go
51 lines (41 loc) · 1.29 KB
/
http_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
// This file is part of *kellner*
//
// Copyright (C) 2015, Travelping GmbH <[email protected]>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package main
import (
"log"
"net/http"
)
const _ExtraLogKey = "kellner-log-data"
// wraps 'orig_handler' to log incoming http-request
func logRequests(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Header.Del(_ExtraLogKey)
statusLog := logStatusCode{ResponseWriter: w}
next.ServeHTTP(&statusLog, r)
if statusLog.Code == 0 {
statusLog.Code = 200
}
if r.TLS == nil || len(r.TLS.PeerCertificates) == 0 {
log.Println(r.RemoteAddr, r.Method, statusLog.Code, r.Host, r.RequestURI, r.Header)
return
}
clientID := clientIDByName(&r.TLS.PeerCertificates[0].Subject)
log.Println(r.RemoteAddr, clientID, r.Method, statusLog.Code, r.Host, r.RequestURI, r.Header)
})
}
//
// small helper to intercept the http-statuscode written
// to the original http.ResponseWriter
type logStatusCode struct {
http.ResponseWriter
Code int
}
func (w *logStatusCode) WriteHeader(code int) {
w.Code = code
w.ResponseWriter.WriteHeader(code)
}