You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a handler that happens to call a 3rd party API in some cases (i.e. to enrich the response with extra data). Sometimes the API call to the 3rd party API fails with 500 even after a few retries -- that's when the handler bails and returns 500 to the fiber API consumer along with an error message.
I've noticed that this error is however not logged by the default logger -- instead just a simple "-" is logged in place of the error message. Based on the docs I'd expect the error to be logged in, but as I said I only get this:
I will assume you want to log to the file
Logger middleware Fiber it just logs http request/response details only
You should use log to observe program behavior
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log" // <=== THIS
"github.com/gofiber/fiber/v2/middleware/logger"
"os"
)
// initialize the logger:
app := fiber.New()
// Create | Open logs.log file
f, err := os.OpenFile(logs.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
return
}
log.SetOutput(f)
app.Use(logger.New())
// handler code that leads to 500
res, err := s.UpdateFoo(ctx, bar)
if err != nil {
log.Error(err) // <=== Log error to file
if code := v1.ErrorCode(err); code == v1.EINVALID {
return c.Status(fiber.StatusBadRequest).JSON(v1.ErrorResponse{
Error: err.Error(),
})
}
return c.Status(fiber.StatusInternalServerError).JSON(v1.ErrorResponse{
Error: err.Error(),
})
}
Gotchya, thanks @rngallen. It's rather confusing to me what is the point of keeping {error} formatter in the default logger -- my expectations are very different, so I'm curious if it plays any role at all.
Question Description
I have a handler that happens to call a 3rd party API in some cases (i.e. to enrich the response with extra data). Sometimes the API call to the 3rd party API fails with 500 even after a few retries -- that's when the handler bails and returns
500
to the fiber API consumer along with an error message.I've noticed that this error is however not logged by the default logger -- instead just a simple "-" is logged in place of the error message. Based on the docs I'd expect the error to be logged in, but as I said I only get this:
Code Snippet (optional)
Checklist:
The text was updated successfully, but these errors were encountered: