From 954ce8ddd99bf7e610f11e47db677d363fc13860 Mon Sep 17 00:00:00 2001 From: "marko.tikvic" Date: Mon, 18 Feb 2019 12:19:32 +0100 Subject: [PATCH] moved mw logging to middleware.go --- middleware.go | 28 ++++++++++++++++++++++++++++ middleware_log.go | 33 --------------------------------- 2 files changed, 28 insertions(+), 33 deletions(-) delete mode 100644 middleware_log.go diff --git a/middleware.go b/middleware.go index 4955a60..f0a6d9c 100644 --- a/middleware.go +++ b/middleware.go @@ -2,8 +2,13 @@ package webutility import ( "net/http" + "time" + + "git.to-net.rs/marko.tikvic/gologger" ) +var httpLogger *gologger.Logger + func SetHeaders(h http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { SetDefaultHeaders(w) @@ -36,6 +41,29 @@ func ParseMultipartForm(h http.HandlerFunc) http.HandlerFunc { } } +func EnableLogging(log string) (err error) { + httpLogger, err = gologger.New(log, gologger.MaxLogSize5MB) + return err +} + +func Log(h http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, req *http.Request) { + t1 := time.Now() + + claims, _ := GetTokenClaims(req) + in := httpLogger.LogHTTPRequest(req, claims.Username) + + rec := NewStatusRecorder(w) + + h(rec, req) + + t2 := time.Now() + out := httpLogger.LogHTTPResponse(rec.Status(), t2.Sub(t1), rec.Size()) + + httpLogger.CombineHTTPLogs(in, out) + } +} + func Auth(roles string, h http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { if _, err := AuthCheck(req, roles); err != nil { diff --git a/middleware_log.go b/middleware_log.go deleted file mode 100644 index cabc647..0000000 --- a/middleware_log.go +++ /dev/null @@ -1,33 +0,0 @@ -package webutility - -import ( - "net/http" - "time" - - "git.to-net.rs/marko.tikvic/gologger" -) - -var httpLogger *gologger.Logger - -func EnableLogging(log string) (err error) { - httpLogger, err = gologger.New(log, gologger.MaxLogSize5MB) - return err -} - -func Log(h http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, req *http.Request) { - t1 := time.Now() - - claims, _ := GetTokenClaims(req) - in := httpLogger.LogHTTPRequest(req, claims.Username) - - rec := NewStatusRecorder(w) - - h(rec, req) - - t2 := time.Now() - out := httpLogger.LogHTTPResponse(rec.Status(), t2.Sub(t1), rec.Size()) - - httpLogger.CombineHTTPLogs(in, out) - } -} -- 1.8.1.2