Merge pull request #525 from fkautz/pr_out_adding_initial_logging_framework_to_api_router

master
Frederick F. Kautz IV 10 years ago
commit b0a4d07736
  1. 1
      .gitignore
  2. 26
      pkg/api/api_router.go

1
.gitignore vendored

@ -9,3 +9,4 @@ site/
**/*.sublime-workspace **/*.sublime-workspace
/.idea/ /.idea/
/Minio.iml /Minio.iml
**/access.log

@ -20,13 +20,14 @@ import (
"log" "log"
"net/http" "net/http"
"bytes"
"encoding/json" "encoding/json"
"fmt"
router "github.com/gorilla/mux" router "github.com/gorilla/mux"
"github.com/minio-io/minio/pkg/api/config" "github.com/minio-io/minio/pkg/api/config"
"github.com/minio-io/minio/pkg/api/quota" "github.com/minio-io/minio/pkg/api/quota"
"github.com/minio-io/minio/pkg/iodine" "github.com/minio-io/minio/pkg/iodine"
"github.com/minio-io/minio/pkg/storage/drivers" "github.com/minio-io/minio/pkg/storage/drivers"
"io"
"os" "os"
"time" "time"
) )
@ -107,6 +108,7 @@ func HTTPHandler(domain string, driver drivers.Driver) http.Handler {
type logHandler struct { type logHandler struct {
http.Handler http.Handler
Logger chan<- string
} }
// LogMessage is a serializable json log message // 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.Request = req
logMessage.Duration = time.Now().Sub(logMessage.StartTime) logMessage.Duration = time.Now().Sub(logMessage.StartTime)
js, _ := json.Marshal(logMessage) js, _ := json.Marshal(logMessage)
fmt.Fprintln(os.Stderr, string(js)) h.Logger <- string(js)
} }
// LogHandler logs requests // LogHandler logs requests
func LogHandler(h http.Handler) http.Handler { 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
} }

Loading…
Cancel
Save