From b90b1f92797444fc750b21a85c8b01e9bb4cb531 Mon Sep 17 00:00:00 2001 From: Lena Fuhrimann <6780471+cloudlena@users.noreply.github.com> Date: Sat, 18 Mar 2017 01:05:21 +0100 Subject: [PATCH] Make logger customizable --- logger.go | 12 +++++------- main.go | 17 +++++++++-------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/logger.go b/logger.go index 1fc8300..ff7ed46 100644 --- a/logger.go +++ b/logger.go @@ -6,20 +6,18 @@ import ( "time" ) -// Logger logs HTTP requests -func Logger() Adapter { +// Logging logs HTTP requests +func Logging(logger *log.Logger) Adapter { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - start := time.Now() - - defer func() { - log.Printf( + defer func(start time.Time) { + logger.Printf( "%s\t%s\t%s", r.Method, r.RequestURI, time.Since(start), ) - }() + }(time.Now()) next.ServeHTTP(w, r) }) diff --git a/main.go b/main.go index 3189d1d..76f4158 100644 --- a/main.go +++ b/main.go @@ -25,19 +25,20 @@ func main() { } router := mux.NewRouter() + logger := log.New(os.Stdout, "request: ", log.Lshortfile) router. Methods("GET"). Path("/"). - Handler(Adapt(IndexHandler(), Logger())) + Handler(Adapt(IndexHandler(), Logging(logger))) router. Methods("GET"). Path("/buckets"). - Handler(Adapt(s.BucketsPageHandler(), Logger())) + Handler(Adapt(s.BucketsPageHandler(), Logging(logger))) router. Methods("GET"). Path("/buckets/{bucketName}"). - Handler(Adapt(s.BucketPageHandler(), Logger())) + Handler(Adapt(s.BucketPageHandler(), Logging(logger))) api := router.PathPrefix("/api").Subrouter() @@ -45,23 +46,23 @@ func main() { buckets. Methods("POST"). Path(""). - Handler(Adapt(s.CreateBucketHandler(), Logger())) + Handler(Adapt(s.CreateBucketHandler(), Logging(logger))) buckets. Methods("DELETE"). Path("/{bucketName}"). - Handler(Adapt(s.DeleteBucketHandler(), Logger())) + Handler(Adapt(s.DeleteBucketHandler(), Logging(logger))) buckets. Methods("POST"). Path("/{bucketName}/objects"). - Handler(Adapt(s.CreateObjectHandler(), Logger())) + Handler(Adapt(s.CreateObjectHandler(), Logging(logger))) buckets. Methods("GET"). Path("/{bucketName}/objects/{objectName}"). - Handler(Adapt(s.GetObjectHandler(), Logger())) + Handler(Adapt(s.GetObjectHandler(), Logging(logger))) buckets. Methods("DELETE"). Path("/{bucketName}/objects/{objectName}"). - Handler(Adapt(s.DeleteObjectHandler(), Logger())) + Handler(Adapt(s.DeleteObjectHandler(), Logging(logger))) log.Fatal(http.ListenAndServe(":"+port, router)) }