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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃 [Question]: How do I get default logger to log error #2927

Open
3 tasks done
milosgajdos opened this issue Mar 19, 2024 · 2 comments
Open
3 tasks done

馃 [Question]: How do I get default logger to log error #2927

milosgajdos opened this issue Mar 19, 2024 · 2 comments

Comments

@milosgajdos
Copy link
Contributor

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:

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

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

@milosgajdos
Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants