Skip to content

Commit

Permalink
Improves Recovery middleware:
Browse files Browse the repository at this point in the history
- request context
- red colouring
  • Loading branch information
manucorporat committed Jan 26, 2016
1 parent 2fc2a3e commit 7afb323
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
6 changes: 4 additions & 2 deletions recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io"
"io/ioutil"
"log"
"net/http/httputil"
"runtime"
)

Expand All @@ -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)

This comment has been minimized.

Copy link
@dustin-decker
}
c.AbortWithStatus(500)
}
Expand Down

0 comments on commit 7afb323

Please sign in to comment.