From 172cd18455b1dc5330630d0615d342214092b942 Mon Sep 17 00:00:00 2001 From: "Frederick F. Kautz IV" Date: Tue, 28 Apr 2015 20:23:17 -0700 Subject: [PATCH] Adding initial logging framework to api_router --- .gitignore | 1 + pkg/api/api_router.go | 26 +++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index c976dd4d8..6b70180ca 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ site/ **/*.sublime-workspace /.idea/ /Minio.iml +**/access.log diff --git a/pkg/api/api_router.go b/pkg/api/api_router.go index 2f64ebe14..8624d2c6a 100644 --- a/pkg/api/api_router.go +++ b/pkg/api/api_router.go @@ -20,13 +20,14 @@ import ( "log" "net/http" + "bytes" "encoding/json" - "fmt" router "github.com/gorilla/mux" "github.com/minio-io/minio/pkg/api/config" "github.com/minio-io/minio/pkg/api/quota" "github.com/minio-io/minio/pkg/iodine" "github.com/minio-io/minio/pkg/storage/drivers" + "io" "os" "time" ) @@ -107,6 +108,7 @@ func HTTPHandler(domain string, driver drivers.Driver) http.Handler { type logHandler struct { http.Handler + Logger chan<- string } // LogMessage is a serializable json log message @@ -141,10 +143,28 @@ func (h *logHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { logMessage.Request = req logMessage.Duration = time.Now().Sub(logMessage.StartTime) js, _ := json.Marshal(logMessage) - fmt.Fprintln(os.Stderr, string(js)) + h.Logger <- string(js) } // LogHandler logs requests func LogHandler(h http.Handler) http.Handler { - return &logHandler{h} + logger, _ := FileLogger("access.log") + return &logHandler{Handler: h, Logger: logger} +} + +// FileLogger returns a channel that is used to write to the logger +func FileLogger(filename string) (chan<- string, error) { + ch := make(chan string) + 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 { + if _, err := io.Copy(file, bytes.NewBufferString(message+"\n")); err != nil { + log.Println(iodine.New(err, nil)) + } + } + }() + return ch, nil }