2014-06-20 19:56:30 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2014-07-15 22:38:56 +00:00
|
|
|
package httplog
|
2014-06-20 19:56:30 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"runtime"
|
|
|
|
"time"
|
2014-06-25 03:51:57 +00:00
|
|
|
|
|
|
|
"github.com/golang/glog"
|
2014-06-20 19:56:30 +00:00
|
|
|
)
|
|
|
|
|
2014-07-15 22:38:56 +00:00
|
|
|
// Handler wraps all HTTP calls to delegate with nice logging.
|
|
|
|
// delegate may use LogOf(w).Addf(...) to write additional info to
|
|
|
|
// the per-request log message.
|
|
|
|
//
|
|
|
|
// Intended to wrap calls to your ServeMux.
|
|
|
|
func Handler(delegate http.Handler, pred StacktracePred) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
2014-08-21 04:27:19 +00:00
|
|
|
defer NewLogged(req, &w).StacktraceWhen(pred).Log()
|
2014-07-15 22:38:56 +00:00
|
|
|
delegate.ServeHTTP(w, req)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// StacktracePred returns true if a stacktrace should be logged for this status.
|
2014-07-02 20:51:27 +00:00
|
|
|
type StacktracePred func(httpStatus int) (logStacktrace bool)
|
|
|
|
|
2014-06-20 19:56:30 +00:00
|
|
|
// Add a layer on top of ResponseWriter, so we can track latency and error
|
|
|
|
// message sources.
|
|
|
|
type respLogger struct {
|
|
|
|
status int
|
|
|
|
statusStack string
|
|
|
|
addedInfo string
|
|
|
|
startTime time.Time
|
|
|
|
|
|
|
|
req *http.Request
|
|
|
|
w http.ResponseWriter
|
2014-07-02 20:51:27 +00:00
|
|
|
|
|
|
|
logStacktracePred StacktracePred
|
|
|
|
}
|
|
|
|
|
2014-07-08 07:09:16 +00:00
|
|
|
// DefaultStacktracePred is the default implementation of StacktracePred.
|
2014-07-02 20:51:27 +00:00
|
|
|
func DefaultStacktracePred(status int) bool {
|
2014-07-15 22:38:56 +00:00
|
|
|
return status < http.StatusOK || status >= http.StatusBadRequest
|
2014-06-20 19:56:30 +00:00
|
|
|
}
|
|
|
|
|
2014-08-21 04:27:19 +00:00
|
|
|
// NewLogged turns a normal response writer into a logged response writer.
|
2014-07-02 20:51:27 +00:00
|
|
|
//
|
2014-06-20 19:56:30 +00:00
|
|
|
// Usage:
|
2014-07-02 20:51:27 +00:00
|
|
|
//
|
2014-08-21 04:27:19 +00:00
|
|
|
// defer NewLogged(req, &w).StacktraceWhen(StatusIsNot(200, 202)).Log()
|
2014-07-02 20:51:27 +00:00
|
|
|
//
|
|
|
|
// (Only the call to Log() is defered, so you can set everything up in one line!)
|
|
|
|
//
|
|
|
|
// Note that this *changes* your writer, to route response writing actions
|
|
|
|
// through the logger.
|
|
|
|
//
|
|
|
|
// Use LogOf(w).Addf(...) to log something along with the response result.
|
2014-08-21 04:27:19 +00:00
|
|
|
func NewLogged(req *http.Request, w *http.ResponseWriter) *respLogger {
|
2014-07-15 22:38:56 +00:00
|
|
|
if _, ok := (*w).(*respLogger); ok {
|
|
|
|
// Don't double-wrap!
|
2014-08-21 04:27:19 +00:00
|
|
|
panic("multiple NewLogged calls!")
|
2014-07-15 22:38:56 +00:00
|
|
|
}
|
2014-07-02 20:51:27 +00:00
|
|
|
rl := &respLogger{
|
|
|
|
startTime: time.Now(),
|
|
|
|
req: req,
|
|
|
|
w: *w,
|
|
|
|
logStacktracePred: DefaultStacktracePred,
|
|
|
|
}
|
|
|
|
*w = rl // hijack caller's writer!
|
|
|
|
return rl
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogOf returns the logger hiding in w. Panics if there isn't such a logger,
|
2014-08-21 04:27:19 +00:00
|
|
|
// because NewLogged() must have been previously called for the log to work.
|
2014-07-02 20:51:27 +00:00
|
|
|
func LogOf(w http.ResponseWriter) *respLogger {
|
|
|
|
if rl, ok := w.(*respLogger); ok {
|
|
|
|
return rl
|
|
|
|
}
|
|
|
|
panic("Logger not installed yet!")
|
|
|
|
}
|
|
|
|
|
2014-07-17 17:05:14 +00:00
|
|
|
// Unlogged returns the original ResponseWriter, or w if it is not our inserted logger.
|
|
|
|
func Unlogged(w http.ResponseWriter) http.ResponseWriter {
|
|
|
|
if rl, ok := w.(*respLogger); ok {
|
|
|
|
return rl.w
|
|
|
|
}
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// StacktraceWhen sets the stacktrace logging predicate, which decides when to log a stacktrace.
|
2014-07-02 20:51:27 +00:00
|
|
|
// There's a default, so you don't need to call this unless you don't like the default.
|
|
|
|
func (rl *respLogger) StacktraceWhen(pred StacktracePred) *respLogger {
|
|
|
|
rl.logStacktracePred = pred
|
|
|
|
return rl
|
|
|
|
}
|
|
|
|
|
|
|
|
// StatusIsNot returns a StacktracePred which will cause stacktraces to be logged
|
|
|
|
// for any status *not* in the given list.
|
|
|
|
func StatusIsNot(statuses ...int) StacktracePred {
|
|
|
|
return func(status int) bool {
|
|
|
|
for _, s := range statuses {
|
|
|
|
if status == s {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2014-06-20 19:56:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// Addf adds additional data to be logged with this request.
|
2014-06-20 19:56:30 +00:00
|
|
|
func (rl *respLogger) Addf(format string, data ...interface{}) {
|
|
|
|
rl.addedInfo += "\n" + fmt.Sprintf(format, data...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log is intended to be called once at the end of your request handler, via defer
|
|
|
|
func (rl *respLogger) Log() {
|
|
|
|
latency := time.Since(rl.startTime)
|
2014-06-25 03:51:57 +00:00
|
|
|
glog.Infof("%s %s: (%v) %v%v%v", rl.req.Method, rl.req.RequestURI, latency, rl.status, rl.statusStack, rl.addedInfo)
|
2014-06-20 19:56:30 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// Header implements http.ResponseWriter.
|
2014-06-20 19:56:30 +00:00
|
|
|
func (rl *respLogger) Header() http.Header {
|
|
|
|
return rl.w.Header()
|
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// Write implements http.ResponseWriter.
|
2014-06-20 19:56:30 +00:00
|
|
|
func (rl *respLogger) Write(b []byte) (int, error) {
|
|
|
|
return rl.w.Write(b)
|
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// WriteHeader implements http.ResponseWriter.
|
2014-06-20 19:56:30 +00:00
|
|
|
func (rl *respLogger) WriteHeader(status int) {
|
|
|
|
rl.status = status
|
2014-07-02 20:51:27 +00:00
|
|
|
if rl.logStacktracePred(status) {
|
2014-06-20 19:56:30 +00:00
|
|
|
// Only log stacks for errors
|
|
|
|
stack := make([]byte, 2048)
|
|
|
|
stack = stack[:runtime.Stack(stack, false)]
|
|
|
|
rl.statusStack = "\n" + string(stack)
|
|
|
|
} else {
|
|
|
|
rl.statusStack = ""
|
|
|
|
}
|
|
|
|
rl.w.WriteHeader(status)
|
|
|
|
}
|