Skip to content

Commit

Permalink
feat: add params to log objects (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidebianchi authored Jun 28, 2021
1 parent 865c369 commit a9f1092
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 10 deletions.
8 changes: 4 additions & 4 deletions lib/custom-logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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'],
Expand All @@ -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: {
Expand All @@ -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'],
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion tests/custom-logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
63 changes: 61 additions & 2 deletions tests/launch-fastify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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()
Expand All @@ -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()

Expand Down
8 changes: 7 additions & 1 deletion tests/log.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@
"url": {
"type": "object",
"properties": {
"path": { "type": "string" }
"path": { "type": "string" },
"params": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
},
"host": {
Expand Down
3 changes: 3 additions & 0 deletions tests/modules/correct-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit a9f1092

Please sign in to comment.