Skip to content

Add support for logLevel plugin's option #494

@Jacopo47

Description

@Jacopo47

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

Looking at fastify's documentation I can found that plugins should accept and propagate (under the correct conditions) logLevel, for example by setting custom log level: https://fastify.dev/docs/latest/Reference/Routes/#custom-log-level

With fastify-static I'm not able to use this feature.
I don't know if I'm doing something wrong but looking to the source code I can actually see that logLevel in not propagate to routes created by this plugin.

Motivation

I'm in a scenario in which clients are calling an API that it's exposing a resource that drive browsers/clients to static contents that we are serving trough fastify-static plugin.

I would like to avoid logging every time that a client is asking for our "main" API other 5, 10, 100 log lines for the static contents that consequentially they will ask to our server.

That's why I would like to server static contents with an higher log level by default.
Then eventually if for debugging purpose we need those log it's just the matter of a configuration change.

Example

In order to reproduce it just populate logLevel into plugin's options:

(starting from: example/server.js)

'use strict'

const path = require('node:path')
const fastify = require('fastify')({ logger: { level: 'trace' } })

fastify
  .register(require('../'), {
    // An absolute path containing static files to serve.
    root: path.join(__dirname, '/public'),
    logLevel: 'warn'
  })
  .listen({ port: 3000 }, err => {
    if (err) throw err
  })

I would expect to not see any log line, instead this is the output:

{"level":30,"time":1735921958098,"pid":8840,"hostname":"ITCLPA70203","msg":"Server listening at http://127.0.0.1:3000"}
{"level":30,"time":1735921959308,"pid":8840,"hostname":"ITCLPA70203","reqId":"req-1","req":{"method":"GET","url":"/index.html","host":"localhost:3000","remoteAddress":"127.0.0.1","remotePort":38648},"msg":"incoming request"}
{"level":30,"time":1735921959314,"pid":8840,"hostname":"ITCLPA70203","reqId":"req-1","res":{"statusCode":304},"responseTime":5.742428999394178,"msg":"request completed"}
{"level":30,"time":1735921959341,"pid":8840,"hostname":"ITCLPA70203","reqId":"req-2","req":{"method":"GET","url":"/index.js","host":"localhost:3000","remoteAddress":"127.0.0.1","remotePort":38648},"msg":"incoming request"}
{"level":30,"time":1735921959343,"pid":8840,"hostname":"ITCLPA70203","reqId":"req-3","req":{"method":"GET","url":"/index.css","host":"localhost:3000","remoteAddress":"127.0.0.1","remotePort":38650},"msg":"incoming request"}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions