-
Why does the Server crash when a log.Fatal is triggered and how to properly log fatal errors without crashing the whole server? package main
import (
"log"
"net/http"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func main() {
e := echo.New()
e.Use(middleware.Recover())
e.GET("/", func(c echo.Context) error {
if true {
log.Fatal("WTF")
}
return c.String(http.StatusOK, "Hello, World!")
})
e.Logger.Fatal(e.Start(":1323"))
} Expected behavior: Internalservererror response |
Beta Was this translation helpful? Give feedback.
Answered by
aldas
Sep 6, 2023
Replies: 1 comment
-
This is because calling https://github.com/golang/go/blob/08e35cc3347f97ef750fd21fbef1061b043580e4/src/log/log.go#L281-L285 // Fatal is equivalent to l.Print() followed by a call to os.Exit(1).
func (l *Logger) Fatal(v ...any) {
l.Output(2, fmt.Sprint(v...))
os.Exit(1)
} p.s. any reasonably ok IDE can show you this - just hold down CTRL and left click on method call and you will be taken to (Goland) or you will see preview of (VScode) that method implementation. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Kirari04
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is because calling
Fatal
will cause application to exit.https://github.com/golang/go/blob/08e35cc3347f97ef750fd21fbef1061b043580e4/src/log/log.go#L281-L285
p.s. any reasonably ok IDE can show you this - just hold down CTRL and left click on method call and you will be taken to (Goland) or you will see preview of (VScode) that method implementation.