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

Fix req object placement #322

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ function pinoLogger (opts, stream) {

log[level](
onRequestErrorObject(req, res, error, {
[reqKey]: req,
[resKey]: res,
[errKey]: error,
[responseTimeKey]: responseTime
Expand All @@ -127,6 +128,7 @@ function pinoLogger (opts, stream) {

log[level](
onRequestSuccessObject(req, res, {
[reqKey]: req,
[resKey]: res,
[responseTimeKey]: responseTime
}),
Expand All @@ -141,7 +143,7 @@ function pinoLogger (opts, stream) {

const log = quietReqLogger ? logger.child({ [requestIdKey]: req.id }) : logger

let fullReqLogger = log.child({ [reqKey]: req })
let fullReqLogger = log
const customPropBindings = (typeof customProps === 'function') ? customProps(req, res) : customProps
if (customPropBindings) {
fullReqLogger = fullReqLogger.child(customPropBindings)
Expand Down
31 changes: 31 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,9 @@ test('uses custom request properties and a receivedMessage callback and the prop
const dest = split(JSON.parse)
const message = DEFAULT_REQUEST_RECEIVED_MSG
const logger = pinoHttp({
customReceivedObject: function (req) {
return { req }
},
Comment on lines +1263 to +1265
Copy link
Author

@stychu stychu Jan 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am bit skeptical about this test. Not sure if this change is ok or I rather should update the source code to include req at this line https://github.com/pinojs/pino-http/blob/master/logger.js#L191 as const receivedObjectResult = onRequestReceivedObject !== undefined ? onRequestReceivedObject(req, res, undefined) : { [reqKey]: req } and remove the addition in the test?

customReceivedMessage: function (_req, _res) {
return message
},
Expand Down Expand Up @@ -1444,3 +1447,31 @@ test('quiet request logging - custom request id key', function (t) {
})
}, handler)
})

test('uses the nested key passed in as an option to hold values', function (t) {
const NESTED_KEY_VALUE = 'someKey'
const dest = split(JSON.parse)
const logger = pinoHttp({ nestedKey: NESTED_KEY_VALUE }, dest)

setup(t, logger, function (err, server) {
t.error(err)
doGet(server)
})

dest.on('data', function (line) {
const nestedObject = line[NESTED_KEY_VALUE]

t.type(nestedObject, 'object', `${NESTED_KEY_VALUE} should exist`)
t.type(nestedObject.req, 'object', `req should be nested under ${NESTED_KEY_VALUE}`)
t.type(nestedObject.res, 'object', `res should be nested under ${NESTED_KEY_VALUE}`)
t.type(nestedObject.responseTime, 'number', `responseTime should be nested under ${NESTED_KEY_VALUE}`)

t.ok(nestedObject, `${NESTED_KEY_VALUE} is defined`)
t.ok(nestedObject.req, `${NESTED_KEY_VALUE}.req is defined`)
t.ok(nestedObject.req, `${NESTED_KEY_VALUE}.res is defined`)
t.notOk(line.req, 'req should be nested under nestedKey')
t.notOk(line.res, 'res should be nested under nestedKey')

t.end()
})
})
Loading