From 4d65bf7d72ac2749a13ab51123791b878fffe216 Mon Sep 17 00:00:00 2001 From: Peter Rabbitson Date: Sat, 29 Apr 2023 12:28:22 +0200 Subject: [PATCH] Add a stacktrace-inducing template token Having `err` objects respond to `%+v` is quite widespread within the golang ecosystem. Add a logger template unit supporting this behavior. --- middleware/logger.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/middleware/logger.go b/middleware/logger.go index 7958d873b..5a222b455 100644 --- a/middleware/logger.go +++ b/middleware/logger.go @@ -3,6 +3,7 @@ package middleware import ( "bytes" "encoding/json" + "fmt" "io" "strconv" "strings" @@ -41,6 +42,7 @@ type ( // - user_agent // - status // - error + // - error_stacktrace (err passed through Sprintf's '%+v') // - latency (In nanoseconds) // - latency_human (Human readable) // - bytes_in (Bytes received) @@ -198,8 +200,12 @@ func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc { if err != nil { // Error may contain invalid JSON e.g. `"` b, _ := json.Marshal(err.Error()) - b = b[1 : len(b)-1] - return buf.Write(b) + return buf.Write(b[1 : len(b)-1]) + } + case "error_stacktrace": + if err != nil { + b, _ := json.Marshal(fmt.Sprintf("%+v", err)) + return buf.Write(b[1 : len(b)-1]) } case "latency": l := stop.Sub(start)