minio/server-api-logging-handlers.go

110 lines
2.9 KiB
Go
Raw Normal View History

2015-04-29 15:24:53 -04:00
/*
* Minio Cloud Storage, (C) 2015 Minio, Inc.
2015-04-29 15:24:53 -04:00
*
* 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 main
2015-04-29 15:24:53 -04:00
import (
"bytes"
"encoding/json"
"io"
"net/http"
"os"
"time"
)
type logHandler struct {
handler http.Handler
logger chan<- []byte
2015-04-29 15:24:53 -04:00
}
// logMessage is a serializable json log message
type logMessage struct {
StartTime time.Time
Duration time.Duration
StatusMessage string // human readable http status message
// 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 {
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) {
w.logMessage.StatusMessage = http.StatusText(status)
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()
2015-05-15 23:44:42 -04:00
}
// 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{
StartTime: time.Now().UTC(),
2015-04-29 15:24:53 -04:00
}
logWriter := &logWriter{responseWriter: w, logMessage: logMessage}
h.handler.ServeHTTP(logWriter, req)
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
logMessage.Duration = time.Now().UTC().Sub(logMessage.StartTime)
2015-04-29 15:24:53 -04:00
js, _ := json.Marshal(logMessage)
js = append(js, byte('\n')) // append a new line
return js
2015-04-29 15:24:53 -04:00
}
// LoggingHandler logs requests
func LoggingHandler(h http.Handler) http.Handler {
logger, _ := fileLogger("access.log")
return &logHandler{handler: h, logger: logger}
2015-04-29 15:24:53 -04:00
}
// fileLogger returns a channel that is used to write to the logger
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, err
2015-04-29 15:24:53 -04:00
}
go func() {
for message := range ch {
if _, err := io.Copy(file, bytes.NewBuffer(message)); err != nil {
2015-09-09 23:28:09 -04:00
// log.Errorln(err)
2015-04-29 15:24:53 -04:00
}
}
}()
return ch, nil
}