2015-04-29 15:24:53 -04:00
|
|
|
/*
|
|
|
|
* Minimalist Object Storage, (C) 2015 Minio, Inc.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package logging
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2015-06-08 05:13:49 -04:00
|
|
|
"strconv"
|
2015-04-29 15:24:53 -04:00
|
|
|
"time"
|
2015-04-30 22:26:23 -04:00
|
|
|
|
2015-06-08 05:13:49 -04:00
|
|
|
"github.com/dustin/go-humanize"
|
2015-05-11 19:23:10 -04:00
|
|
|
"github.com/minio/minio/pkg/iodine"
|
|
|
|
"github.com/minio/minio/pkg/utils/log"
|
2015-04-29 15:24:53 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type logHandler struct {
|
|
|
|
http.Handler
|
2015-06-08 05:13:49 -04:00
|
|
|
Logger chan<- []byte
|
2015-04-29 15:24:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// LogMessage is a serializable json log message
|
|
|
|
type LogMessage struct {
|
2015-06-08 05:13:49 -04:00
|
|
|
StartTime time.Time
|
|
|
|
Duration time.Duration
|
|
|
|
StatusMessage string // human readable http status message
|
|
|
|
ContentLength string // human readable content length
|
|
|
|
|
|
|
|
// HTTP detailed message
|
|
|
|
HTTP struct {
|
|
|
|
ResponseHeaders http.Header
|
|
|
|
Request *http.Request
|
|
|
|
}
|
2015-04-29 15:24:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// LogWriter is used to capture status for log messages
|
|
|
|
type LogWriter struct {
|
2015-05-15 23:44:42 -04:00
|
|
|
ResponseWriter http.ResponseWriter
|
|
|
|
LogMessage *LogMessage
|
2015-04-29 15:24:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteHeader writes headers and stores status in LogMessage
|
|
|
|
func (w *LogWriter) WriteHeader(status int) {
|
2015-06-08 05:13:49 -04:00
|
|
|
w.LogMessage.StatusMessage = http.StatusText(status)
|
2015-04-29 15:24:53 -04:00
|
|
|
w.ResponseWriter.WriteHeader(status)
|
2015-05-15 23:44:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Header Dummy wrapper for LogWriter
|
|
|
|
func (w *LogWriter) Header() http.Header {
|
|
|
|
return w.ResponseWriter.Header()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write Dummy wrapper for LogWriter
|
|
|
|
func (w *LogWriter) Write(data []byte) (int, error) {
|
|
|
|
return w.ResponseWriter.Write(data)
|
2015-04-29 15:24:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *logHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|
|
|
logMessage := &LogMessage{
|
2015-04-30 22:26:23 -04:00
|
|
|
StartTime: time.Now().UTC(),
|
2015-04-29 15:24:53 -04:00
|
|
|
}
|
|
|
|
logWriter := &LogWriter{ResponseWriter: w, LogMessage: logMessage}
|
2015-05-24 23:51:11 -04:00
|
|
|
h.Handler.ServeHTTP(logWriter, req)
|
2015-06-08 05:13:49 -04:00
|
|
|
h.Logger <- getLogMessage(logMessage, w, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getLogMessage(logMessage *LogMessage, w http.ResponseWriter, req *http.Request) []byte {
|
|
|
|
// store lower level details
|
|
|
|
logMessage.HTTP.ResponseHeaders = w.Header()
|
|
|
|
logMessage.HTTP.Request = req
|
|
|
|
|
|
|
|
// humanize content-length to be printed in logs
|
|
|
|
contentLength, _ := strconv.Atoi(logMessage.HTTP.ResponseHeaders.Get("Content-Length"))
|
|
|
|
logMessage.ContentLength = humanize.IBytes(uint64(contentLength))
|
2015-04-30 22:26:23 -04:00
|
|
|
logMessage.Duration = time.Now().UTC().Sub(logMessage.StartTime)
|
2015-04-29 15:24:53 -04:00
|
|
|
js, _ := json.Marshal(logMessage)
|
2015-06-08 05:13:49 -04:00
|
|
|
js = append(js, byte('\n')) // append a new line
|
|
|
|
return js
|
2015-04-29 15:24:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// LogHandler logs requests
|
|
|
|
func LogHandler(h http.Handler) http.Handler {
|
|
|
|
logger, _ := FileLogger("access.log")
|
|
|
|
return &logHandler{Handler: h, Logger: logger}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FileLogger returns a channel that is used to write to the logger
|
2015-06-08 05:13:49 -04:00
|
|
|
func FileLogger(filename string) (chan<- []byte, error) {
|
|
|
|
ch := make(chan []byte)
|
2015-04-29 15:24:53 -04:00
|
|
|
file, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return nil, iodine.New(err, map[string]string{"logfile": filename})
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
for message := range ch {
|
2015-06-08 05:13:49 -04:00
|
|
|
if _, err := io.Copy(file, bytes.NewBuffer(message)); err != nil {
|
2015-04-29 15:24:53 -04:00
|
|
|
log.Println(iodine.New(err, nil))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return ch, nil
|
|
|
|
}
|