2015-09-12 13:10:03 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
2016-05-12 14:32:12 +00:00
|
|
|
"bufio"
|
2016-04-19 23:45:59 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
2015-09-12 13:10:03 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2016-04-19 23:45:59 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
2016-08-15 02:50:09 +00:00
|
|
|
|
2016-08-18 12:20:11 +00:00
|
|
|
"github.com/containous/traefik/log"
|
2017-05-09 12:02:44 +00:00
|
|
|
"github.com/containous/traefik/middlewares/accesslog"
|
2016-08-15 02:50:09 +00:00
|
|
|
"github.com/streamrail/concurrent-map"
|
2016-04-19 23:45:59 +00:00
|
|
|
)
|
2015-09-24 15:16:13 +00:00
|
|
|
|
2016-04-19 23:45:59 +00:00
|
|
|
const (
|
|
|
|
loggerReqidHeader = "X-Traefik-Reqid"
|
2015-09-12 13:10:03 +00:00
|
|
|
)
|
|
|
|
|
2016-04-28 10:53:02 +00:00
|
|
|
/*
|
2016-04-28 11:00:38 +00:00
|
|
|
Logger writes each request and its response to the access log.
|
2016-04-28 10:53:02 +00:00
|
|
|
It gets some information from the logInfoResponseWriter set up by previous middleware.
|
|
|
|
*/
|
2015-09-12 13:10:03 +00:00
|
|
|
type Logger struct {
|
|
|
|
file *os.File
|
|
|
|
}
|
|
|
|
|
2016-04-19 23:45:59 +00:00
|
|
|
// Logging handler to log frontend name, backend name, and elapsed time
|
|
|
|
type frontendBackendLoggingHandler struct {
|
|
|
|
reqid string
|
|
|
|
writer io.Writer
|
|
|
|
handlerFunc http.HandlerFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
reqidCounter uint64 // Request ID
|
|
|
|
infoRwMap = cmap.New() // Map of reqid to response writer
|
2016-04-20 18:54:57 +00:00
|
|
|
backend2FrontendMap *map[string]string
|
2016-04-19 23:45:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// logInfoResponseWriter is a wrapper of type http.ResponseWriter
|
|
|
|
// that tracks frontend and backend names and request status and size
|
|
|
|
type logInfoResponseWriter struct {
|
|
|
|
rw http.ResponseWriter
|
|
|
|
backend string
|
|
|
|
frontend string
|
|
|
|
status int
|
|
|
|
size int
|
|
|
|
}
|
|
|
|
|
2015-11-01 18:34:54 +00:00
|
|
|
// NewLogger returns a new Logger instance.
|
2015-09-12 13:10:03 +00:00
|
|
|
func NewLogger(file string) *Logger {
|
2015-09-15 20:32:09 +00:00
|
|
|
if len(file) > 0 {
|
|
|
|
fi, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
2015-09-12 13:10:03 +00:00
|
|
|
if err != nil {
|
2016-08-19 08:36:54 +00:00
|
|
|
log.Error("Error opening file", err)
|
2015-09-12 13:10:03 +00:00
|
|
|
}
|
|
|
|
return &Logger{fi}
|
|
|
|
}
|
2015-09-24 15:16:13 +00:00
|
|
|
return &Logger{nil}
|
2015-09-12 13:10:03 +00:00
|
|
|
}
|
|
|
|
|
2016-04-19 23:45:59 +00:00
|
|
|
// SetBackend2FrontendMap is called by server.go to set up frontend translation
|
|
|
|
func SetBackend2FrontendMap(newMap *map[string]string) {
|
2016-04-20 18:54:57 +00:00
|
|
|
backend2FrontendMap = newMap
|
2016-04-19 23:45:59 +00:00
|
|
|
}
|
|
|
|
|
2015-09-12 13:10:03 +00:00
|
|
|
func (l *Logger) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
2015-09-15 20:32:09 +00:00
|
|
|
if l.file == nil {
|
2015-09-12 13:10:03 +00:00
|
|
|
next(rw, r)
|
2015-09-15 20:32:09 +00:00
|
|
|
} else {
|
2016-04-19 23:45:59 +00:00
|
|
|
reqid := strconv.FormatUint(atomic.AddUint64(&reqidCounter, 1), 10)
|
|
|
|
r.Header[loggerReqidHeader] = []string{reqid}
|
|
|
|
defer deleteReqid(r, reqid)
|
|
|
|
frontendBackendLoggingHandler{reqid, l.file, next}.ServeHTTP(rw, r)
|
2015-09-12 13:10:03 +00:00
|
|
|
}
|
2015-09-12 13:20:56 +00:00
|
|
|
}
|
|
|
|
|
2016-04-19 23:45:59 +00:00
|
|
|
// Delete a reqid from the map and the request's headers
|
|
|
|
func deleteReqid(r *http.Request, reqid string) {
|
|
|
|
infoRwMap.Remove(reqid)
|
|
|
|
delete(r.Header, loggerReqidHeader)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the backend name for the Logger
|
|
|
|
func saveBackendNameForLogger(r *http.Request, backendName string) {
|
|
|
|
if reqidHdr := r.Header[loggerReqidHeader]; len(reqidHdr) == 1 {
|
|
|
|
reqid := reqidHdr[0]
|
2016-04-20 01:25:22 +00:00
|
|
|
if infoRw, ok := infoRwMap.Get(reqid); ok {
|
|
|
|
infoRw.(*logInfoResponseWriter).SetBackend(backendName)
|
2016-04-20 18:54:57 +00:00
|
|
|
infoRw.(*logInfoResponseWriter).SetFrontend((*backend2FrontendMap)[backendName])
|
2016-04-19 23:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the Logger (i.e. the file).
|
2015-09-12 13:20:56 +00:00
|
|
|
func (l *Logger) Close() {
|
2016-04-13 18:36:23 +00:00
|
|
|
if l.file != nil {
|
|
|
|
l.file.Close()
|
|
|
|
}
|
2015-09-15 20:32:09 +00:00
|
|
|
}
|
2016-04-19 23:45:59 +00:00
|
|
|
|
|
|
|
// Logging handler to log frontend name, backend name, and elapsed time
|
|
|
|
func (fblh frontendBackendLoggingHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
startTime := time.Now()
|
|
|
|
infoRw := &logInfoResponseWriter{rw: rw}
|
|
|
|
infoRwMap.Set(fblh.reqid, infoRw)
|
|
|
|
fblh.handlerFunc(infoRw, req)
|
|
|
|
|
|
|
|
username := "-"
|
|
|
|
url := *req.URL
|
|
|
|
if url.User != nil {
|
|
|
|
if name := url.User.Username(); name != "" {
|
|
|
|
username = name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
host, _, err := net.SplitHostPort(req.RemoteAddr)
|
|
|
|
if err != nil {
|
|
|
|
host = req.RemoteAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
ts := startTime.Format("02/Jan/2006:15:04:05 -0700")
|
|
|
|
method := req.Method
|
|
|
|
uri := url.RequestURI()
|
|
|
|
if qmIndex := strings.Index(uri, "?"); qmIndex > 0 {
|
|
|
|
uri = uri[0:qmIndex]
|
|
|
|
}
|
|
|
|
proto := req.Proto
|
|
|
|
referer := req.Referer()
|
|
|
|
agent := req.UserAgent()
|
|
|
|
|
2017-05-09 12:02:44 +00:00
|
|
|
logTable := accesslog.GetLogDataTable(req)
|
|
|
|
frontend := logTable.Core[accesslog.FrontendName]
|
|
|
|
backend := logTable.Core[accesslog.BackendURL]
|
2016-04-19 23:45:59 +00:00
|
|
|
status := infoRw.GetStatus()
|
|
|
|
size := infoRw.GetSize()
|
|
|
|
|
|
|
|
elapsed := time.Now().UTC().Sub(startTime.UTC())
|
2016-08-15 02:50:09 +00:00
|
|
|
elapsedMillis := elapsed.Nanoseconds() / 1000000
|
|
|
|
fmt.Fprintf(fblh.writer, `%s - %s [%s] "%s %s %s" %d %d "%s" "%s" %s "%s" "%s" %dms%s`,
|
|
|
|
host, username, ts, method, uri, proto, status, size, referer, agent, fblh.reqid, frontend, backend, elapsedMillis, "\n")
|
2016-04-19 23:45:59 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lirw *logInfoResponseWriter) Header() http.Header {
|
|
|
|
return lirw.rw.Header()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lirw *logInfoResponseWriter) Write(b []byte) (int, error) {
|
|
|
|
if lirw.status == 0 {
|
|
|
|
lirw.status = http.StatusOK
|
|
|
|
}
|
|
|
|
size, err := lirw.rw.Write(b)
|
|
|
|
lirw.size += size
|
|
|
|
return size, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lirw *logInfoResponseWriter) WriteHeader(s int) {
|
|
|
|
lirw.rw.WriteHeader(s)
|
|
|
|
lirw.status = s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lirw *logInfoResponseWriter) Flush() {
|
|
|
|
f, ok := lirw.rw.(http.Flusher)
|
|
|
|
if ok {
|
|
|
|
f.Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-12 14:32:12 +00:00
|
|
|
func (lirw *logInfoResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
|
|
return lirw.rw.(http.Hijacker).Hijack()
|
|
|
|
}
|
|
|
|
|
2016-04-19 23:45:59 +00:00
|
|
|
func (lirw *logInfoResponseWriter) GetStatus() int {
|
|
|
|
return lirw.status
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lirw *logInfoResponseWriter) GetSize() int {
|
|
|
|
return lirw.size
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lirw *logInfoResponseWriter) GetBackend() string {
|
|
|
|
return lirw.backend
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lirw *logInfoResponseWriter) GetFrontend() string {
|
|
|
|
return lirw.frontend
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lirw *logInfoResponseWriter) SetBackend(backend string) {
|
|
|
|
lirw.backend = backend
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lirw *logInfoResponseWriter) SetFrontend(frontend string) {
|
|
|
|
lirw.frontend = frontend
|
|
|
|
}
|