fiber icon indicating copy to clipboard operation
fiber copied to clipboard

🤗 [Question]: How do I get default logger to log error

Open milosgajdos opened this issue 2 years ago • 4 comments

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:

17:23:14 | 500 | 17.382402041s | ::1 | PUT | /api/v1/foo/bar | -

Code Snippet (optional)

// initialize the logger:
app := fiber.New()
app.Use(logger.New())

        // handler code that leads to 500
	res, err := s.UpdateFoo(ctx, bar)
	if err != nil {
		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(),
		})
	}

Checklist:

  • [X] I agree to follow Fiber's Code of Conduct.
  • [X] I have checked for existing issues that describe my questions prior to opening this one.
  • [X] I understand that improperly formatted questions may be closed without explanation.

milosgajdos avatar Mar 19 '24 18:03 milosgajdos

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(),
		})
	}

rngallen avatar Mar 19 '24 20:03 rngallen

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.

milosgajdos avatar Mar 20 '24 15:03 milosgajdos

@milosgajdos That only works if you return an error, example:

app.Get("/", func(_ fiber.Ctx) error {
    return errors.New("some random error")
})

In your case, you are creating a JSON, then returning that.

gaby avatar Jul 09 '24 12:07 gaby