You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
2.9 KiB
110 lines
2.9 KiB
10 years ago
|
/*
|
||
10 years ago
|
* Minio Cloud Storage, (C) 2015 Minio, Inc.
|
||
10 years ago
|
*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
9 years ago
|
package main
|
||
10 years ago
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/json"
|
||
|
"io"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type logHandler struct {
|
||
10 years ago
|
handler http.Handler
|
||
|
logger chan<- []byte
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// logMessage is a serializable json log message
|
||
|
type logMessage struct {
|
||
10 years ago
|
StartTime time.Time
|
||
|
Duration time.Duration
|
||
|
StatusMessage string // human readable http status message
|
||
|
|
||
|
// HTTP detailed message
|
||
|
HTTP struct {
|
||
|
ResponseHeaders http.Header
|
||
|
Request *http.Request
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// logWriter is used to capture status for log messages
|
||
|
type logWriter struct {
|
||
|
responseWriter http.ResponseWriter
|
||
|
logMessage *logMessage
|
||
10 years ago
|
}
|
||
|
|
||
|
// WriteHeader writes headers and stores status in LogMessage
|
||
10 years ago
|
func (w *logWriter) WriteHeader(status int) {
|
||
|
w.logMessage.StatusMessage = http.StatusText(status)
|
||
|
w.responseWriter.WriteHeader(status)
|
||
10 years ago
|
}
|
||
|
|
||
|
// Header Dummy wrapper for LogWriter
|
||
10 years ago
|
func (w *logWriter) Header() http.Header {
|
||
|
return w.responseWriter.Header()
|
||
10 years ago
|
}
|
||
|
|
||
|
// Write Dummy wrapper for LogWriter
|
||
10 years ago
|
func (w *logWriter) Write(data []byte) (int, error) {
|
||
|
return w.responseWriter.Write(data)
|
||
10 years ago
|
}
|
||
|
|
||
|
func (h *logHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||
10 years ago
|
logMessage := &logMessage{
|
||
10 years ago
|
StartTime: time.Now().UTC(),
|
||
10 years ago
|
}
|
||
10 years ago
|
logWriter := &logWriter{responseWriter: w, logMessage: logMessage}
|
||
|
h.handler.ServeHTTP(logWriter, req)
|
||
|
h.logger <- getLogMessage(logMessage, w, req)
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
func getLogMessage(logMessage *logMessage, w http.ResponseWriter, req *http.Request) []byte {
|
||
10 years ago
|
// store lower level details
|
||
|
logMessage.HTTP.ResponseHeaders = w.Header()
|
||
|
logMessage.HTTP.Request = req
|
||
|
|
||
10 years ago
|
logMessage.Duration = time.Now().UTC().Sub(logMessage.StartTime)
|
||
10 years ago
|
js, _ := json.Marshal(logMessage)
|
||
10 years ago
|
js = append(js, byte('\n')) // append a new line
|
||
|
return js
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// LoggingHandler logs requests
|
||
|
func LoggingHandler(h http.Handler) http.Handler {
|
||
10 years ago
|
logger, _ := fileLogger("access.log")
|
||
|
return &logHandler{handler: h, logger: logger}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// fileLogger returns a channel that is used to write to the logger
|
||
|
func fileLogger(filename string) (chan<- []byte, error) {
|
||
10 years ago
|
ch := make(chan []byte)
|
||
10 years ago
|
file, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
|
||
|
if err != nil {
|
||
9 years ago
|
return nil, err
|
||
10 years ago
|
}
|
||
|
go func() {
|
||
|
for message := range ch {
|
||
10 years ago
|
if _, err := io.Copy(file, bytes.NewBuffer(message)); err != nil {
|
||
9 years ago
|
// log.Errorln(err)
|
||
10 years ago
|
}
|
||
|
}
|
||
|
}()
|
||
|
return ch, nil
|
||
|
}
|