diff --git a/_examples/http_request/request-logger/main.go b/_examples/http_request/request-logger/main.go index 3eb64b43..327b3e69 100644 --- a/_examples/http_request/request-logger/main.go +++ b/_examples/http_request/request-logger/main.go @@ -22,7 +22,10 @@ func main() { // if !empty then its contents derives from `ctx.Values().Get("logger_message") // will be added to the logs. - MessageContextKey: "logger_message", + MessageContextKeys: []string{"logger_message"}, + + // if !empty then its contents derives from `ctx.GetHeader("User-Agent") + MessageHeaderKeys: []string{"User-Agent"}, }) app.Use(customLogger) diff --git a/middleware/logger/config.go b/middleware/logger/config.go index 2b324024..eb3e2143 100644 --- a/middleware/logger/config.go +++ b/middleware/logger/config.go @@ -37,7 +37,7 @@ type Config struct { // Defaults to false. Columns bool - // MessageContextKey if not empty, + // MessageContextKeys if not empty, // the middleware will try to fetch // the contents with `ctx.Values().Get(MessageContextKey)` // and if available then these contents will be @@ -46,12 +46,23 @@ type Config struct { // a new column will be added named 'Message'. // // Defaults to empty. - MessageContextKey string + MessageContextKeys []string + + // MessageHeaderKeys if not empty, + // the middleware will try to fetch + // the contents with `ctx.Values().Get(MessageHeaderKey)` + // and if available then these contents will be + // appended as part of the logs (with `%v`, in order to be able to set a struct too), + // if Columns field was setted to true then + // a new column will be added named 'HeaderMessage'. + // + // Defaults to empty. + MessageHeaderKeys []string // LogFunc is the writer which logs are written to, // if missing the logger middleware uses the app.Logger().Infof instead. // Note that message argument can be empty. - LogFunc func(now time.Time, latency time.Duration, status, ip, method, path string, message interface{}) + LogFunc func(now time.Time, latency time.Duration, status, ip, method, path string, message interface{}, headerMessage interface{}) // Skippers used to skip the logging i.e by `ctx.Path()` and serve // the next/main handler immediately. Skippers []SkipperFunc @@ -66,15 +77,14 @@ type Config struct { // LogFunc and Skippers to nil as well. func DefaultConfig() Config { return Config{ - Status: true, - IP: true, - Method: true, - Path: true, - Columns: false, - MessageContextKey: "", - LogFunc: nil, - Skippers: nil, - skip: nil, + Status: true, + IP: true, + Method: true, + Path: true, + Columns: false, + LogFunc: nil, + Skippers: nil, + skip: nil, } } diff --git a/middleware/logger/logger.go b/middleware/logger/logger.go index b4796484..e9abeb36 100644 --- a/middleware/logger/logger.go +++ b/middleware/logger/logger.go @@ -70,19 +70,37 @@ func (l *requestLoggerMiddleware) ServeHTTP(ctx context.Context) { } var message interface{} - if ctxKey := l.config.MessageContextKey; ctxKey != "" { - message = ctx.Values().Get(ctxKey) + if ctxKeys := l.config.MessageContextKeys; len(ctxKeys) > 0 { + for _, key := range ctxKeys { + msg := ctx.Values().Get(key) + if message == nil { + message = msg + } else { + message = fmt.Sprintf(" %v %v", message, msg) + } + } + } + var headerMessage interface{} + if headerKeys := l.config.MessageHeaderKeys; len(headerKeys) > 0 { + for _, key := range headerKeys { + msg := ctx.GetHeader(key) + if headerMessage == nil { + headerMessage = msg + } else { + headerMessage = fmt.Sprintf(" %v %v", headerMessage, msg) + } + } } // print the logs if logFunc := l.config.LogFunc; logFunc != nil { - logFunc(endTime, latency, status, ip, method, path, message) + logFunc(endTime, latency, status, ip, method, path, message, headerMessage) return } if l.config.Columns { endTimeFormatted := endTime.Format("2006/01/02 - 15:04:05") - output := Columnize(endTimeFormatted, latency, status, ip, method, path, message) + output := Columnize(endTimeFormatted, latency, status, ip, method, path, message, headerMessage) ctx.Application().Logger().Printer.Output.Write([]byte(output)) return } @@ -91,12 +109,16 @@ func (l *requestLoggerMiddleware) ServeHTTP(ctx context.Context) { if message != nil { line += fmt.Sprintf(" %v", message) } + + if headerMessage != nil { + line += fmt.Sprintf(" %v", headerMessage) + } ctx.Application().Logger().Info(line) } // Columnize formats the given arguments as columns and returns the formatted output, // note that it appends a new line to the end. -func Columnize(nowFormatted string, latency time.Duration, status, ip, method, path string, message interface{}) string { +func Columnize(nowFormatted string, latency time.Duration, status, ip, method, path string, message interface{}, headerMessage interface{}) string { titles := "Time | Status | Latency | IP | Method | Path" line := fmt.Sprintf("%s | %v | %4v | %s | %s | %s", nowFormatted, status, latency, ip, method, path) @@ -105,6 +127,11 @@ func Columnize(nowFormatted string, latency time.Duration, status, ip, method, p line += fmt.Sprintf(" | %v", message) } + if headerMessage != nil { + titles += " | HeaderMessage" + line += fmt.Sprintf(" | %v", headerMessage) + } + outputC := []string{ titles, line,