@ -26,16 +26,17 @@ import (
"github.com/gorilla/mux"
"github.com/gorilla/mux"
"github.com/minio/minio/cmd/logger/message/audit"
"github.com/minio/minio/cmd/logger/message/audit"
stats "github.com/minio/minio/cmd/http/stats"
)
)
// ResponseWriter - is a wrapper to trap the http response status code.
// ResponseWriter - is a wrapper to trap the http response status code.
type ResponseWriter struct {
type ResponseWriter struct {
http . ResponseWriter
http . ResponseWriter
StatusCode int
StatusCode int
// Response body should be logged
// Log body of 4xx or 5xx responses
LogBody bool
LogErrBody bool
// Log body of all responses
LogAllBody bool
TimeToFirstByte time . Duration
TimeToFirstByte time . Duration
StartTime time . Time
StartTime time . Time
// number of bytes written
// number of bytes written
@ -69,7 +70,7 @@ func (lrw *ResponseWriter) Write(p []byte) (int, error) {
lrw . writeHeaders ( & lrw . headers , http . StatusOK , lrw . Header ( ) )
lrw . writeHeaders ( & lrw . headers , http . StatusOK , lrw . Header ( ) )
lrw . headersLogged = true
lrw . headersLogged = true
}
}
if lrw . StatusCode >= http . StatusBadRequest || lrw . LogBody {
if ( lrw . LogErrBody && lrw . StatusCode >= http . StatusBadRequest ) || lrw . LogAll Body {
// Always logging error responses.
// Always logging error responses.
lrw . body . Write ( p )
lrw . body . Write ( p )
}
}
@ -96,7 +97,7 @@ var BodyPlaceHolder = []byte("<BODY>")
func ( lrw * ResponseWriter ) Body ( ) [ ] byte {
func ( lrw * ResponseWriter ) Body ( ) [ ] byte {
// If there was an error response or body logging is enabled
// If there was an error response or body logging is enabled
// then we return the body contents
// then we return the body contents
if lrw . StatusCode >= http . StatusBadRequest || lrw . LogBody {
if ( lrw . LogErrBody && lrw . StatusCode >= http . StatusBadRequest ) || lrw . LogAll Body {
return lrw . body . Bytes ( )
return lrw . body . Bytes ( )
}
}
// ... otherwise we return the <BODY> place holder
// ... otherwise we return the <BODY> place holder
@ -145,11 +146,11 @@ func AuditLog(w http.ResponseWriter, r *http.Request, api string, reqClaims map[
timeToFirstByte time . Duration
timeToFirstByte time . Duration
)
)
st , ok := w . ( * stats . RecordAPIStats )
st , ok := w . ( * ResponseWriter )
if ok {
if ok {
statusCode = st . Resp StatusCode
statusCode = st . StatusCode
timeToResponse = time . Now ( ) . UTC ( ) . Sub ( st . StartTime )
timeToResponse = time . Now ( ) . UTC ( ) . Sub ( st . StartTime )
timeToFirstByte = st . TTFB
timeToFirstByte = st . Time To First Byte
}
}
vars := mux . Vars ( r )
vars := mux . Vars ( r )