diff --git a/lib/custom-logger.js b/lib/custom-logger.js index bbad1ab..6b55e7b 100644 --- a/lib/custom-logger.js +++ b/lib/custom-logger.js @@ -49,7 +49,7 @@ function removePort(host) { } function logIncomingRequest(req, reply, next) { - const { method, url, headers } = req + const { method, url, headers, params } = req req.log.trace({ http: { request: { @@ -59,7 +59,7 @@ function logIncomingRequest(req, reply, next) { }, }, }, - url: { path: url }, + url: { path: url, params }, host: { hostname: removePort(headers['host']), forwardedHostame: headers['x-forwarded-host'], @@ -70,7 +70,7 @@ function logIncomingRequest(req, reply, next) { } function logRequestCompleted(req, reply, next) { - const { method, url, headers } = req + const { method, url, headers, params } = req const { statusCode } = reply req.log.info({ http: { @@ -87,7 +87,7 @@ function logRequestCompleted(req, reply, next) { }, }, }, - url: { path: url }, + url: { path: url, params }, host: { hostname: removePort(headers['host']), forwardedHost: headers['x-forwarded-host'], diff --git a/package.json b/package.json index ebb0b2b..e0d0c3e 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "commander": "^7.2.0", "dotenv": "^8.2.0", "dotenv-expand": "^5.1.0", - "fastify": "^3.14.2", + "fastify": "^3.18.0", "fastify-metrics": "^7.1.0", "fastify-plugin": "^3.0.0", "fastify-sensible": "^3.1.1", @@ -55,7 +55,7 @@ "ajv": "^6.12.5", "eslint": "^7.24.0", "split2": "^3.2.2", - "tap": "^15.0.3" + "tap": "^15.0.9" }, "engines": { "node": ">=10" diff --git a/tests/custom-logger.test.js b/tests/custom-logger.test.js index f838db5..3cbb563 100644 --- a/tests/custom-logger.test.js +++ b/tests/custom-logger.test.js @@ -60,7 +60,7 @@ test('Test generation custom logger default options', assert => { assert.end() }) -test('Test generation custom logger default options', assert => { +test('Test generation custom logger custom options', assert => { const options = { logLevel: 'debug', redact: 'ignoredValue', diff --git a/tests/launch-fastify.test.js b/tests/launch-fastify.test.js index a3aaf5a..86e666a 100644 --- a/tests/launch-fastify.test.js +++ b/tests/launch-fastify.test.js @@ -207,7 +207,7 @@ test('Test custom serializers', t => { userAgent: { original: 'lightMyRequest' }, }, }) - assert.strictSame(line.url, { path: '/' }) + assert.strictSame(line.url, { path: '/', params: {} }) assert.strictSame(line.host, { hostname: 'testHost', forwardedHostame: 'testForwardedHost', ip: 'testIp' }) stream.once('data', secondLine => { @@ -225,7 +225,7 @@ test('Test custom serializers', t => { body: { bytes: 13 }, }, }) - assert.strictSame(secondLine.url, { path: '/' }) + assert.strictSame(secondLine.url, { path: '/', params: {} }) assert.strictSame(secondLine.host, { hostname: 'testHost', forwardedHost: 'testForwardedHost', ip: 'testIp' }) assert.end() @@ -251,6 +251,65 @@ test('Test custom serializers', t => { await fastifyInstance.close() }) + t.test('fields values - path with params', async assert => { + assert.plan(13) + const stream = split(JSON.parse) + + stream.once('data', () => { + stream.once('data', line => { + assert.equal(line.reqId, '34') + assert.equal(line.level, 10) + assert.notOk(line.req) + assert.strictSame(line.http, { + request: { + method: 'GET', + userAgent: { original: 'lightMyRequest' }, + }, + }) + assert.strictSame(line.url, { path: '/items/my-item', params: { itemId: 'my-item' } }) + assert.strictSame(line.host, { hostname: 'testHost', forwardedHostame: 'testForwardedHost', ip: 'testIp' }) + + stream.once('data', secondLine => { + assert.equal(line.reqId, '34') + assert.equal(secondLine.level, 30) + assert.notOk(secondLine.res) + assert.ok(secondLine.responseTime) + assert.strictSame(secondLine.http, { + request: { + method: 'GET', + userAgent: { original: 'lightMyRequest' }, + }, + response: { + statusCode: 200, + body: { bytes: 13 }, + }, + }) + assert.strictSame(secondLine.url, { path: '/items/my-item', params: { itemId: 'my-item' } }) + assert.strictSame(secondLine.host, { hostname: 'testHost', forwardedHost: 'testForwardedHost', ip: 'testIp' }) + + assert.end() + }) + }) + }) + + const fastifyInstance = await launch('./tests/modules/correct-module', { + logLevel: 'trace', + stream, + }) + await fastifyInstance.inject({ + method: 'GET', + url: '/items/my-item', + headers: { + 'x-forwarded-for': 'testIp', + 'host': 'testHost:3000', + 'x-forwarded-host': 'testForwardedHost', + 'x-request-id': '34', + }, + }) + + await fastifyInstance.close() + }) + t.test('matches schema', async assert => { const ajv = new Ajv() diff --git a/tests/log.schema.json b/tests/log.schema.json index 8ed16cc..68afd75 100644 --- a/tests/log.schema.json +++ b/tests/log.schema.json @@ -31,7 +31,13 @@ "url": { "type": "object", "properties": { - "path": { "type": "string" } + "path": { "type": "string" }, + "params": { + "type": "object", + "additionalProperties": { + "type": "string" + } + } } }, "host": { diff --git a/tests/modules/correct-module.js b/tests/modules/correct-module.js index f6aef1d..0607f6c 100644 --- a/tests/modules/correct-module.js +++ b/tests/modules/correct-module.js @@ -33,6 +33,9 @@ module.exports = async function plugin(fastify, config) { reply.header('Content-Length', '') reply.send({ hi: 'there' }) }) + fastify.get('/items/:itemId', function emptyContentLength(request, reply) { + reply.send({ config }) + }) } module.exports.options = {