From f84248cb4e0e173f472919316cd86018181c9b40 Mon Sep 17 00:00:00 2001 From: Gerasimos Maropoulos Date: Thu, 31 May 2018 01:39:23 +0300 Subject: [PATCH] middleware/logger: new configuration field, defaults to false: `Query bool`, if true prints the full path, including the URL query as requested at https://github.com/kataras/iris/issues/1017 Former-commit-id: 03c8fc523a8ba955dae43e4c7e9498fc3d86a1c8 --- _examples/http_request/request-logger/main.go | 2 ++ .../request-logger/request-logger-file/main.go | 2 +- middleware/logger/config.go | 7 +++++++ middleware/logger/logger.go | 6 +++++- 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/_examples/http_request/request-logger/main.go b/_examples/http_request/request-logger/main.go index 327b3e6999..888de7a6c7 100644 --- a/_examples/http_request/request-logger/main.go +++ b/_examples/http_request/request-logger/main.go @@ -17,6 +17,8 @@ func main() { Method: true, // Path displays the request path Path: true, + // Query appends the url query to the Path. + Query: true, //Columns: true, diff --git a/_examples/http_request/request-logger/request-logger-file/main.go b/_examples/http_request/request-logger/request-logger-file/main.go index 86e68cba0c..dc37f8a16b 100644 --- a/_examples/http_request/request-logger/request-logger-file/main.go +++ b/_examples/http_request/request-logger/request-logger-file/main.go @@ -85,7 +85,7 @@ func newRequestLogger() (h iris.Handler, close func() error) { return err } - c.LogFunc = func(now time.Time, latency time.Duration, status, ip, method, path string, message interface{},headerMessage interface{}) { + c.LogFunc = func(now time.Time, latency time.Duration, status, ip, method, path string, message interface{}, headerMessage interface{}) { output := logger.Columnize(now.Format("2006/01/02 - 15:04:05"), latency, status, ip, method, path, message, headerMessage) logFile.Write([]byte(output)) } diff --git a/middleware/logger/config.go b/middleware/logger/config.go index eb3e2143ef..991e57b454 100644 --- a/middleware/logger/config.go +++ b/middleware/logger/config.go @@ -30,6 +30,12 @@ type Config struct { // Defaults to true. Path bool + // Query will append the URL Query to the Path. + // Path should be true too. + // + // Defaults to false. + Query bool + // Columns will display the logs as a formatted columns-rows text (bool). // If custom `LogFunc` has been provided then this field is useless and users should // use the `Columinize` function of the logger to get the output result as columns. @@ -81,6 +87,7 @@ func DefaultConfig() Config { IP: true, Method: true, Path: true, + Query: false, Columns: false, LogFunc: nil, Skippers: nil, diff --git a/middleware/logger/logger.go b/middleware/logger/logger.go index e9abeb36e7..98d5c8fc4f 100644 --- a/middleware/logger/logger.go +++ b/middleware/logger/logger.go @@ -66,7 +66,11 @@ func (l *requestLoggerMiddleware) ServeHTTP(ctx context.Context) { } if l.config.Path { - path = ctx.Path() + if l.config.Query { + path = ctx.Request().URL.RequestURI() + } else { + path = ctx.Path() + } } var message interface{}