Skip to content

Unable to get the correct status code in fiber middleware #895

@akkuman

Description

@akkuman
func NewRouter(
	app *fiber.App,
	logger logrus.FieldLogger,
) {
	app.Use(middleware.Logger(logger))
...
	apiConfig.Transformers = append(apiConfig.Transformers, ErrorResponseTransformer)
	humaAPI := humafiber.New(app, apiConfig)
func Logger(l logrus.FieldLogger) func(c *fiber.Ctx) error {
	return func(ctx *fiber.Ctx) error {
		var timestamp atomic.Value
		// Create correct timeformat
		timestamp.Store(time.Now().In(time.Local).Format(time.RFC3339))

		reqStart := time.Now()

		err := ctx.Next()

		reqStop := time.Now()
		reqLatency := reqStop.Sub(reqStart)

		l.WithFields(
			logrus.Fields{
				"ip":        ctx.IP(),
				"method":    ctx.Method(),
				"url":       ctx.OriginalURL(),
				"status":    ctx.Response().StatusCode(),
				"bytesSend": ctx.Response().Header.ContentLength(),
				"bytesRecv": ctx.Request().Header.ContentLength(),
				"ua":        string(ctx.Request().Header.UserAgent()),
				"latency":   reqLatency.String(),
				"reqTime":   timestamp.Load().(string),
			},
		).Info("request finished")

		return err
	}
}

If a path that is not registered in huma is requested, the http request returns a status code 404, but the "request finished" log records a status code 200.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions