fastify-cors icon indicating copy to clipboard operation
fastify-cors copied to clipboard

logLevel option not respected

Open vatosarmat opened this issue 1 year ago • 5 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.28.1

Plugin version

9.0.1

Node.js version

18.20.2

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

Ubuntu 22.04.4 LTS

Description

It would be nice to exclude OPTIONS requests from the logs

Link to code that reproduces the bug

No response

Expected Behavior

No response

vatosarmat avatar Sep 19 '24 08:09 vatosarmat

There is not an instance of logging in this module https://github.com/search?q=repo%3Afastify%2Ffastify-cors%20log&type=code

jsumners avatar Sep 19 '24 11:09 jsumners

This module registers route handler and fastify logs requests to the route like it does for any route. But for user-defined route it is possible to customize log level and mute particular routes requests:

fastify.get('/', { logLevel: 'warn' }, (request, reply) => {
  reply.send({ hello: 'world' })
})

Yet, there is no way to push logLevel option into the route registered by this module.

  // The preflight reply must occur in the hook. This allows fastify-cors to reply to
  // preflight requests BEFORE possible authentication plugins. If the preflight reply
  // occurred in this handler, other plugins may deny the request since the browser will
  // remove most headers (such as the Authentication header).
  //
  // This route simply enables fastify to accept preflight requests.
  fastify.options('*', { schema: { hide: hideOptionsRoute } }, (req, reply) => {
    if (!req.corsPreflightEnabled) {
      // Do not handle preflight requests if the origin option disabled CORS
      reply.callNotFound()
      return
    }

    reply.send()
  })

vatosarmat avatar Sep 19 '24 13:09 vatosarmat

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

mcollina avatar Sep 28 '24 09:09 mcollina

Example: https://github.com/fastify/csrf-protection/pull/186

Eomm avatar Mar 28 '25 14:03 Eomm

I am working on this issue

gulbaki avatar Jun 20 '25 06:06 gulbaki