Skip to content
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

How to get Recover Stack log information #2089

Closed
OneSeven opened this issue Feb 6, 2022 · 4 comments
Closed

How to get Recover Stack log information #2089

OneSeven opened this issue Feb 6, 2022 · 4 comments

Comments

@OneSeven
Copy link

OneSeven commented Feb 6, 2022

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

@aldas
Copy link
Contributor

aldas commented Feb 6, 2022

There are some improvements currently in latest but those are not released yet. Please see #2072 for alternatives until v4.7.0 will be released.

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"))
}

@OneSeven
Copy link
Author

OneSeven commented Feb 6, 2022

thanks.
Is it possible to add a configuration to set the format of the Stack log output?
When will 4.7 be released?

@OneSeven
Copy link
Author

OneSeven commented Feb 7, 2022

@aldas Hi,4.7 verison Is it possible to add a configuration to set the format of the Stack log output?

@aldas
Copy link
Contributor

aldas commented Feb 7, 2022

v4.7.0 allows you to log stack in any format you like. You need to implement your own logging function

This is where it is called:

if config.LogErrorFunc != nil {
err = config.LogErrorFunc(c, err, stack)
} else if !config.DisablePrintStack {

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"))
}

@OneSeven OneSeven closed this as completed Feb 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants