-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
How to get Recover Stack log information #2089
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
There are some improvements currently in latest but those are not released yet. Please see #2072 for alternatives until This is pretty much same functionality as recovery mw has (except stacktrace) func main() {
e := echo.New()
e.Use(middleware.Logger())
customRecovery := func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) (err error) {
defer func() {
if r := recover(); r != nil {
rErr, ok := r.(error)
if !ok {
rErr = fmt.Errorf("%v", r)
}
// c.Logger().Error(rErr) // you do not need to log here. Logger middleware is up in chain.
err = rErr // allows upstream middlewares to receive recovered error
}
}()
return next(c)
}
}
e.Use(customRecovery)
e.GET("/", func(c echo.Context) error {
panic(errors.New("test"))
})
log.Fatal(e.Start(":8080"))
} |
thanks. |
@aldas Hi,4.7 verison Is it possible to add a configuration to set the format of the Stack log output? |
This is where it is called: Lines 93 to 95 in 4a1ccdf
Meanwhile you can create your own middleware as: func main() {
e := echo.New()
e.Use(middleware.Logger())
customRecovery := func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) (err error) {
defer func() {
if r := recover(); r != nil {
rErr, ok := r.(error)
if !ok {
rErr = fmt.Errorf("%v", r)
}
stack = make([]byte, 4000)
length = runtime.Stack(stack, true)
// replace with whatever logging library you use
c.Logger().Print(fmt.Sprintf("[PANIC RECOVER] %v %s\n", err, stack[:length]))
err = rErr // allows upstream middlewares to receive recovered error
}
}()
return next(c)
}
}
e.Use(customRecovery)
e.GET("/", func(c echo.Context) error {
panic(errors.New("test"))
})
log.Fatal(e.Start(":8080"))
} |
The output of the Recover Stack log is unformatted json information, and I can't see the problem intuitively. And Recover does not have a custom handler
The text was updated successfully, but these errors were encountered: