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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add logLevel option #502

Merged
merged 11 commits into from
Feb 2, 2025
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ Default: `{}`
Constraints to add to registered routes. See Fastify's documentation for
[route constraints](https://fastify.dev/docs/latest/Reference/Routes/#constraints).

#### `logLevel`

Default: `info`

Set log level for registered routes.

#### `prefixAvoidTrailingSlash`

Default: `false`
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ async function fastifyStatic (fastify, opts) {
schema: {
hide: opts.schemaHide !== undefined ? opts.schemaHide : true
},
logLevel: opts.logLevel,
errorHandler (error, request, reply) {
if (error?.code === 'ERR_STREAM_PREMATURE_CLOSE') {
reply.request.raw.destroy()
Expand Down
121 changes: 121 additions & 0 deletions test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3492,3 +3492,124 @@ test('respect the .type when using with sendFile with contentType disabled', asy
t.assert.deepStrictEqual(response.headers.get('content-length'), contentLength)
t.assert.deepStrictEqual(await response.text(), fooContent)
})

test('register /static/ with custom log level', async t => {
t.plan(9)

const pluginOptions = {
root: path.join(__dirname, '/static'),
prefix: '/static/',
logLevel: 'warn'
}
const fastify = Fastify({
logger: {
stream: {
write: (logLine) => {
if (logLine.includes('"msg":"incoming request"')) {
console.warn(logLine)
throw new Error('Should never reach this point since log level is set at WARN!! Unexpected log line: ' + logLine)
}
},
},
},
})
fastify.register(fastifyStatic, pluginOptions)

t.after(() => fastify.close())

await fastify.listen({ port: 0 })
fastify.server.unref()

await t.test('/static/index.html', async (t) => {
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)

const response = await fetch('http://localhost:' + fastify.server.address().port + '/static/index.html')
t.assert.ok(response.ok)
t.assert.deepStrictEqual(response.status, 200)
t.assert.deepStrictEqual(await response.text(), indexContent)
genericResponseChecks(t, response)
})

await t.test('/static/index.html', async t => {
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
const response = await fetch('http://localhost:' + fastify.server.address().port + '/static/index.html', { method: 'HEAD' })
t.assert.ok(response.ok)
t.assert.deepStrictEqual(response.status, 200)
t.assert.deepStrictEqual(await response.text(), '')
genericResponseChecks(t, response)
})

await t.test('/static/index.css', async (t) => {
t.plan(2 + GENERIC_RESPONSE_CHECK_COUNT)
const response = await fetch('http://localhost:' + fastify.server.address().port + '/static/index.css')
t.assert.ok(response.ok)
t.assert.deepStrictEqual(response.status, 200)
genericResponseChecks(t, response)
})

await t.test('/static/', async (t) => {
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
const response = await fetch('http://localhost:' + fastify.server.address().port + '/static/')

t.assert.ok(response.ok)
t.assert.deepStrictEqual(response.status, 200)
t.assert.deepStrictEqual(await response.text(), indexContent)
genericResponseChecks(t, response)
})

await t.test('/static/deep/path/for/test/purpose/foo.html', async (t) => {
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
const response = await fetch('http://localhost:' + fastify.server.address().port + '/static/deep/path/for/test/purpose/foo.html')

t.assert.ok(response.ok)
t.assert.deepStrictEqual(response.status, 200)
t.assert.deepStrictEqual(await response.text(), deepContent)
genericResponseChecks(t, response)
})
await t.test('/static/deep/path/for/test/', async (t) => {
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
const response = await fetch('http://localhost:' + fastify.server.address().port + '/static/deep/path/for/test/')

t.assert.ok(response.ok)
t.assert.deepStrictEqual(response.status, 200)
t.assert.deepStrictEqual(await response.text(), innerIndex)
genericResponseChecks(t, response)
})

await t.test('/static/this/path/for/test', async (t) => {
t.plan(2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT)
const response = await fetch('http://localhost:' + fastify.server.address().port + '/static/this/path/for/test')

t.assert.ok(!response.ok)
t.assert.deepStrictEqual(response.status, 404)
genericErrorResponseChecks(t, response)
})

await t.test('/static/this/path/doesnt/exist.html', async (t) => {
t.plan(2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT)
const response = await fetch('http://localhost:' + fastify.server.address().port + '/static/this/path/doesnt/exist.html')

t.assert.ok(!response.ok)
t.assert.deepStrictEqual(response.status, 404)
genericErrorResponseChecks(t, response)
})

await t.test('304', async t => {
t.plan(5 + GENERIC_RESPONSE_CHECK_COUNT)
const response = await fetch('http://localhost:' + fastify.server.address().port + '/static/index.html')

t.assert.ok(response.ok)
t.assert.deepStrictEqual(response.status, 200)
t.assert.deepStrictEqual(await response.text(), indexContent)
genericResponseChecks(t, response)

const response2 = await fetch('http://localhost:' + fastify.server.address().port + '/static/index.html', {
headers: {
'if-none-match': response.headers.get('etag')
},
cache: 'no-cache'
})
t.assert.ok(!response2.ok)
t.assert.deepStrictEqual(response2.status, 304)
})
})
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ declare namespace fastifyStatic {
lastModified?: boolean;
maxAge?: string | number;
constraints?: RouteOptions['constraints'];
logLevel?: RouteOptions['logLevel'];
}

export const fastifyStatic: FastifyStaticPlugin
Expand Down
3 changes: 2 additions & 1 deletion types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ const options: FastifyStaticOptions = {
constraints: {
host: /.*\.example\.com/,
version: '1.0.2'
}
},
logLevel: 'warn'
}

expectError<FastifyStaticOptions>({
Expand Down
Loading