Skip to content

Commit bcf294f

Browse files
authored
feat: add logLevel option (#502)
1 parent e5faab5 commit bcf294f

File tree

5 files changed

+131
-1
lines changed

5 files changed

+131
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ Default: `{}`
136136
Constraints to add to registered routes. See Fastify's documentation for
137137
[route constraints](https://fastify.dev/docs/latest/Reference/Routes/#constraints).
138138

139+
#### `logLevel`
140+
141+
Default: `info`
142+
143+
Set log level for registered routes.
144+
139145
#### `prefixAvoidTrailingSlash`
140146

141147
Default: `false`

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ async function fastifyStatic (fastify, opts) {
6464
schema: {
6565
hide: opts.schemaHide !== undefined ? opts.schemaHide : true
6666
},
67+
logLevel: opts.logLevel,
6768
errorHandler (error, request, reply) {
6869
if (error?.code === 'ERR_STREAM_PREMATURE_CLOSE') {
6970
reply.request.raw.destroy()

test/static.test.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3492,3 +3492,124 @@ test('respect the .type when using with sendFile with contentType disabled', asy
34923492
t.assert.deepStrictEqual(response.headers.get('content-length'), contentLength)
34933493
t.assert.deepStrictEqual(await response.text(), fooContent)
34943494
})
3495+
3496+
test('register /static/ with custom log level', async t => {
3497+
t.plan(9)
3498+
3499+
const pluginOptions = {
3500+
root: path.join(__dirname, '/static'),
3501+
prefix: '/static/',
3502+
logLevel: 'warn'
3503+
}
3504+
const fastify = Fastify({
3505+
logger: {
3506+
stream: {
3507+
write: (logLine) => {
3508+
if (logLine.includes('"msg":"incoming request"')) {
3509+
console.warn(logLine)
3510+
throw new Error('Should never reach this point since log level is set at WARN!! Unexpected log line: ' + logLine)
3511+
}
3512+
},
3513+
},
3514+
},
3515+
})
3516+
fastify.register(fastifyStatic, pluginOptions)
3517+
3518+
t.after(() => fastify.close())
3519+
3520+
await fastify.listen({ port: 0 })
3521+
fastify.server.unref()
3522+
3523+
await t.test('/static/index.html', async (t) => {
3524+
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
3525+
3526+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/static/index.html')
3527+
t.assert.ok(response.ok)
3528+
t.assert.deepStrictEqual(response.status, 200)
3529+
t.assert.deepStrictEqual(await response.text(), indexContent)
3530+
genericResponseChecks(t, response)
3531+
})
3532+
3533+
await t.test('/static/index.html', async t => {
3534+
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
3535+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/static/index.html', { method: 'HEAD' })
3536+
t.assert.ok(response.ok)
3537+
t.assert.deepStrictEqual(response.status, 200)
3538+
t.assert.deepStrictEqual(await response.text(), '')
3539+
genericResponseChecks(t, response)
3540+
})
3541+
3542+
await t.test('/static/index.css', async (t) => {
3543+
t.plan(2 + GENERIC_RESPONSE_CHECK_COUNT)
3544+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/static/index.css')
3545+
t.assert.ok(response.ok)
3546+
t.assert.deepStrictEqual(response.status, 200)
3547+
genericResponseChecks(t, response)
3548+
})
3549+
3550+
await t.test('/static/', async (t) => {
3551+
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
3552+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/static/')
3553+
3554+
t.assert.ok(response.ok)
3555+
t.assert.deepStrictEqual(response.status, 200)
3556+
t.assert.deepStrictEqual(await response.text(), indexContent)
3557+
genericResponseChecks(t, response)
3558+
})
3559+
3560+
await t.test('/static/deep/path/for/test/purpose/foo.html', async (t) => {
3561+
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
3562+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/static/deep/path/for/test/purpose/foo.html')
3563+
3564+
t.assert.ok(response.ok)
3565+
t.assert.deepStrictEqual(response.status, 200)
3566+
t.assert.deepStrictEqual(await response.text(), deepContent)
3567+
genericResponseChecks(t, response)
3568+
})
3569+
await t.test('/static/deep/path/for/test/', async (t) => {
3570+
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
3571+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/static/deep/path/for/test/')
3572+
3573+
t.assert.ok(response.ok)
3574+
t.assert.deepStrictEqual(response.status, 200)
3575+
t.assert.deepStrictEqual(await response.text(), innerIndex)
3576+
genericResponseChecks(t, response)
3577+
})
3578+
3579+
await t.test('/static/this/path/for/test', async (t) => {
3580+
t.plan(2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT)
3581+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/static/this/path/for/test')
3582+
3583+
t.assert.ok(!response.ok)
3584+
t.assert.deepStrictEqual(response.status, 404)
3585+
genericErrorResponseChecks(t, response)
3586+
})
3587+
3588+
await t.test('/static/this/path/doesnt/exist.html', async (t) => {
3589+
t.plan(2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT)
3590+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/static/this/path/doesnt/exist.html')
3591+
3592+
t.assert.ok(!response.ok)
3593+
t.assert.deepStrictEqual(response.status, 404)
3594+
genericErrorResponseChecks(t, response)
3595+
})
3596+
3597+
await t.test('304', async t => {
3598+
t.plan(5 + GENERIC_RESPONSE_CHECK_COUNT)
3599+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/static/index.html')
3600+
3601+
t.assert.ok(response.ok)
3602+
t.assert.deepStrictEqual(response.status, 200)
3603+
t.assert.deepStrictEqual(await response.text(), indexContent)
3604+
genericResponseChecks(t, response)
3605+
3606+
const response2 = await fetch('http://localhost:' + fastify.server.address().port + '/static/index.html', {
3607+
headers: {
3608+
'if-none-match': response.headers.get('etag')
3609+
},
3610+
cache: 'no-cache'
3611+
})
3612+
t.assert.ok(!response2.ok)
3613+
t.assert.deepStrictEqual(response2.status, 304)
3614+
})
3615+
})

types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ declare namespace fastifyStatic {
114114
lastModified?: boolean;
115115
maxAge?: string | number;
116116
constraints?: RouteOptions['constraints'];
117+
logLevel?: RouteOptions['logLevel'];
117118
}
118119

119120
export const fastifyStatic: FastifyStaticPlugin

types/index.test-d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ const options: FastifyStaticOptions = {
6868
constraints: {
6969
host: /.*\.example\.com/,
7070
version: '1.0.2'
71-
}
71+
},
72+
logLevel: 'warn'
7273
}
7374

7475
expectError<FastifyStaticOptions>({

0 commit comments

Comments
 (0)