From 7afb3238a3b703864c785ce626e0c7330a9c559b Mon Sep 17 00:00:00 2001 From: "Manu Mtz.-Almeida" Date: Tue, 26 Jan 2016 18:35:56 +0100 Subject: [PATCH] Improves Recovery middleware: - request context - red colouring --- gin.go | 2 +- recovery.go | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/gin.go b/gin.go index 3834d67..25f3f69 100644 --- a/gin.go +++ b/gin.go @@ -113,7 +113,7 @@ func New() *Engine { // Default returns an Engine instance with the Logger and Recovery middleware already attached. func Default() *Engine { engine := New() - engine.Use(Recovery(), Logger()) + engine.Use(Logger(), Recovery()) return engine } diff --git a/recovery.go b/recovery.go index e296e33..50a73b6 100644 --- a/recovery.go +++ b/recovery.go @@ -10,6 +10,7 @@ import ( "io" "io/ioutil" "log" + "net/http/httputil" "runtime" ) @@ -28,14 +29,15 @@ func Recovery() HandlerFunc { func RecoveryWithWriter(out io.Writer) HandlerFunc { var logger *log.Logger if out != nil { - logger = log.New(out, "", log.LstdFlags) + logger = log.New(out, "\n\n\x1b[31m", log.LstdFlags) } return func(c *Context) { defer func() { if err := recover(); err != nil { if logger != nil { stack := stack(3) - logger.Printf("Panic recovery -> %s\n%s\n", err, stack) + httprequest, _ := httputil.DumpRequest(c.Request, false) + logger.Printf("[Recovery] panic recovered:\n%s\n%s\n%s %s", string(httprequest), err, stack, reset) } c.AbortWithStatus(500) }