fastify / fastify-cors

Fastify CORS
MIT License
414 stars 57 forks source link

logLevel option not respected #320

Open vatosarmat opened 5 days ago

vatosarmat commented 5 days ago

Prerequisites

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

jsumners commented 5 days ago

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

vatosarmat commented 5 days ago

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