From 2bdf2ba00e91036c127cef54a634274c2d7819fc Mon Sep 17 00:00:00 2001 From: Shraddha Falane Date: Thu, 25 Aug 2022 17:11:30 +0530 Subject: [PATCH 1/2] feat: configurable reqlog middleware to log request IP, headers, params --- extra/reqlog/middleware.go | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/extra/reqlog/middleware.go b/extra/reqlog/middleware.go index 7e0dcbc..2afe988 100644 --- a/extra/reqlog/middleware.go +++ b/extra/reqlog/middleware.go @@ -15,8 +15,9 @@ import ( ) type middleware struct { - enabled bool - verbose bool + enabled bool + verbose bool + enabledOtherParameter bool } type Option func(m *middleware) @@ -35,11 +36,20 @@ func WithVerbose(on bool) Option { } } +// WithEnabledOtherParameter configures the middleware to log request Path, IP, Request header and Params. +func WithEnabledOtherParameter(on bool) Option { + return func(m *middleware) { + m.enabledOtherParameter = on + } +} + // WithEnv configures the middleware using the environment variable value. // For example, WithEnv("BUNDEBUG"): // - BUNDEBUG=0 - disables the middleware. // - BUNDEBUG=1 - enables the middleware. // - BUNDEBUG=2 - enables the middleware and verbose mode. +// - BUNDEBUG=3 - enables the middleware and logs request heraders , path, ip and params. + func FromEnv(key string) Option { if key == "" { key = "BUNDEBUG" @@ -48,14 +58,16 @@ func FromEnv(key string) Option { if env, ok := os.LookupEnv(key); ok { m.enabled = env != "" && env != "0" m.verbose = env == "2" + m.enabledOtherParameter = env == "3" } } } func NewMiddleware(opts ...Option) bunrouter.MiddlewareFunc { c := &middleware{ - enabled: true, - verbose: true, + enabled: true, + verbose: true, + enabledOtherParameter: false, } for _, opt := range opts { opt(c) @@ -94,9 +106,12 @@ func (m *middleware) Middleware(next bunrouter.HandlerFunc) bunrouter.HandlerFun formatStatus(statusCode), fmt.Sprintf(" %10s ", dur.Round(time.Microsecond)), formatMethod(req.Method), - req.URL.String(), ) + if m.enabledOtherParameter { + args = append(args, req.URL.String(), req.RemoteAddr) + } + if err != nil { typ := reflect.TypeOf(err).String() args = append(args, @@ -106,6 +121,12 @@ func (m *middleware) Middleware(next bunrouter.HandlerFunc) bunrouter.HandlerFun } fmt.Println(args...) + if m.enabledOtherParameter { + fmt.Println("User-Agent:", req.Header.Get("User-Agent")) + fmt.Println("Referer:", req.Header.Get("Referer")) + fmt.Println("Query Params:", req.URL.Query()) + fmt.Println("Body Params:", req.PostForm) + } return err } From 9af1da80adbe16d2e06b098809454f95c36d7590 Mon Sep 17 00:00:00 2001 From: Shraddha Falane Date: Thu, 1 Sep 2022 18:09:26 +0530 Subject: [PATCH 2/2] feat: req log middleware with individual options to log parameters --- extra/reqlog/middleware.go | 62 ++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 19 deletions(-) diff --git a/extra/reqlog/middleware.go b/extra/reqlog/middleware.go index 2e0d729..cf17a28 100644 --- a/extra/reqlog/middleware.go +++ b/extra/reqlog/middleware.go @@ -15,9 +15,11 @@ import ( ) type middleware struct { - enabled bool - verbose bool - enabledOtherParameter bool + enabled bool + verbose bool + enabledReferer bool + enabledRemoteAddr bool + enabledQuery bool } type Option func(m *middleware) @@ -36,10 +38,24 @@ func WithVerbose(on bool) Option { } } -// WithEnabledOtherParameter configures the middleware to log request Path, IP, Request header and Params. -func WithEnabledOtherParameter(on bool) Option { +// WithEnabledReferer configures the middleware to log request referer. +func WithEnabledReferer(on bool) Option { return func(m *middleware) { - m.enabledOtherParameter = on + m.enabledReferer = on + } +} + +// WithEnabledRemoteAddr configures the middleware to log request address. +func WithEnabledRemoteAddr(on bool) Option { + return func(m *middleware) { + m.enabledRemoteAddr = on + } +} + +// WithEnabledQuery configures the middleware to log request query params. +func WithEnabledQuery(on bool) Option { + return func(m *middleware) { + m.enabledQuery = on } } @@ -48,7 +64,9 @@ func WithEnabledOtherParameter(on bool) Option { // - BUNDEBUG=0 - disables the middleware. // - BUNDEBUG=1 - enables the middleware. // - BUNDEBUG=2 - enables the middleware and verbose mode. -// - BUNDEBUG=3 - enables the middleware and logs request headers , path, ip and params. +// - BUNDEBUG=3 - enables the middleware and logs request referer. +// - BUNDEBUG=4 - enables the middleware and logs request remote address. +// - BUNDEBUG=5 - enables the middleware and logs request query params. func FromEnv(keys ...string) Option { if len(keys) == 0 { @@ -59,7 +77,9 @@ func FromEnv(keys ...string) Option { if env, ok := os.LookupEnv(key); ok { m.enabled = env != "" && env != "0" m.verbose = env == "2" - m.enabledOtherParameter = env == "3" + m.enabledReferer = env == "3" + m.enabledRemoteAddr = env == "4" + m.enabledQuery = env == "5" break } } @@ -68,9 +88,11 @@ func FromEnv(keys ...string) Option { func NewMiddleware(opts ...Option) bunrouter.MiddlewareFunc { c := &middleware{ - enabled: true, - verbose: true, - enabledOtherParameter: false, + enabled: true, + verbose: true, + enabledReferer: false, + enabledRemoteAddr: false, + enabledQuery: false, } for _, opt := range opts { opt(c) @@ -111,8 +133,16 @@ func (m *middleware) Middleware(next bunrouter.HandlerFunc) bunrouter.HandlerFun formatMethod(req.Method), ) - if m.enabledOtherParameter { - args = append(args, req.URL.String(), req.RemoteAddr) + if m.enabledReferer { + args = append(args, req.Header.Get("Referer")) + } + + if m.enabledRemoteAddr { + args = append(args, req.URL.String()) + } + + if m.enabledQuery { + args = append(args, req.URL.Query()) } if err != nil { @@ -124,12 +154,6 @@ func (m *middleware) Middleware(next bunrouter.HandlerFunc) bunrouter.HandlerFun } fmt.Println(args...) - if m.enabledOtherParameter { - fmt.Println("User-Agent:", req.Header.Get("User-Agent")) - fmt.Println("Referer:", req.Header.Get("Referer")) - fmt.Println("Query Params:", req.URL.Query()) - fmt.Println("Body Params:", req.PostForm) - } return err }