1
0
mirror of https://github.com/containous/traefik.git synced 2024-12-27 03:21:40 +03:00
traefik/middlewares/logger.go

42 lines
837 B
Go
Raw Normal View History

2015-09-12 16:10:03 +03:00
package middlewares
import (
"log"
"net/http"
"os"
"github.com/gorilla/handlers"
2015-09-12 16:10:03 +03:00
)
// Logger is a middleware handler that logs the request as it goes in and the response as it goes out.
type Logger struct {
file *os.File
}
// NewLogger returns a new Logger instance.
2015-09-12 16:10:03 +03:00
func NewLogger(file string) *Logger {
if len(file) > 0 {
fi, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
2015-09-12 16:10:03 +03:00
if err != nil {
log.Fatal("Error opening file", err)
}
return &Logger{fi}
}
return &Logger{nil}
2015-09-12 16:10:03 +03:00
}
func (l *Logger) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
if l.file == nil {
2015-09-12 16:10:03 +03:00
next(rw, r)
} else {
2015-09-12 16:10:03 +03:00
handlers.CombinedLoggingHandler(l.file, next).ServeHTTP(rw, r)
}
2015-09-12 16:20:56 +03:00
}
// Close closes the logger (i.e. the file).
2015-09-12 16:20:56 +03:00
func (l *Logger) Close() {
if l.file != nil {
l.file.Close()
}
}