minor refactoring, commenting
parent
65e502b8e7
commit
4c99eef8af
|
@ -114,15 +114,9 @@ func cors(inner http.Handler) http.Handler {
|
|||
func logging(inner http.Handler, name string, weblog *log.Logger) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
|
||||
record := &responseLogger{
|
||||
w: w,
|
||||
}
|
||||
|
||||
inner.ServeHTTP(record, r)
|
||||
|
||||
logLine := buildLogLine(record, r, start)
|
||||
|
||||
l := &responseLogger{w: w}
|
||||
inner.ServeHTTP(l, r)
|
||||
logLine := buildLogLine(l, r, start)
|
||||
weblog.Println(logLine)
|
||||
})
|
||||
}
|
||||
|
@ -130,17 +124,11 @@ func logging(inner http.Handler, name string, weblog *log.Logger) http.Handler {
|
|||
func recovery(inner http.Handler, name string, weblog *log.Logger) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
|
||||
record := &responseLogger{
|
||||
w: w,
|
||||
}
|
||||
|
||||
inner.ServeHTTP(record, r)
|
||||
|
||||
l := &responseLogger{w: w}
|
||||
inner.ServeHTTP(l, r)
|
||||
if err := recover(); err != nil {
|
||||
logLine := buildLogLine(record, r, start)
|
||||
logLine := buildLogLine(l, r, start)
|
||||
logLine = fmt.Sprintf(`%s [err:%s]`, logLine, err)
|
||||
|
||||
weblog.Println(logLine)
|
||||
}
|
||||
})
|
||||
|
|
|
@ -27,7 +27,7 @@ func (l *responseLogger) Header() http.Header {
|
|||
|
||||
func (l *responseLogger) Write(b []byte) (int, error) {
|
||||
if l.status == 0 {
|
||||
// The status will be StatusOK if WriteHeader has not been called yet
|
||||
// Set status if WriteHeader has not been called
|
||||
l.status = http.StatusOK
|
||||
}
|
||||
size, err := l.w.Write(b)
|
||||
|
@ -49,17 +49,8 @@ func (l *responseLogger) Size() int {
|
|||
}
|
||||
|
||||
// Common Log Format: http://en.wikipedia.org/wiki/Common_Log_Format
|
||||
// 127.0.0.1 user-identifier frank [10/Oct/2000:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326
|
||||
// A "-" in a field indicates missing data.[citation needed]
|
||||
|
||||
// 127.0.0.1 is the IP address of the client (remote host) which made the request to the server.
|
||||
// user-identifier is the RFC 1413 identity of the client.
|
||||
// frank is the userid of the person requesting the document.
|
||||
// [10/Oct/2000:13:55:36 -0700] is the date, time, and time zone when the server finished processing the request, by default in strftime format %d/%b/%Y:%H:%M:%S %z.
|
||||
// "GET /apache_pb.gif HTTP/1.0" is the request line from the client. The method GET, /apache_pb.gif the resource requested, and HTTP/1.0 the HTTP protocol.
|
||||
// 200 is the HTTP status code returned to the client. 2xx is a successful response, 3xx a redirection, 4xx a client error, and 5xx a server error.
|
||||
// 2326 is the size of the object returned to the client, measured in bytes.
|
||||
|
||||
// buildLogLine creates a common log format, plus referrer and user agent at the end
|
||||
func buildLogLine(l *responseLogger, r *http.Request, start time.Time) string {
|
||||
username := "-"
|
||||
url := r.URL
|
||||
|
|
Loading…
Reference in New Issue