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

either reqCustomProps or customProps are valid #122

Merged
merged 4 commits into from
Feb 4, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ $ node example.js | pino-pretty
* `customErrorMessage`: set to a `function (err, res) => { /* returns message string */ }` This function will be invoked at each failed response, setting "msg" property to returned string. If not set, default value will be used.
* `customAttributeKeys`: allows the log object attributes added by `pino-http` to be given custom keys. Accepts an object of format `{ [original]: [override] }`. Attributes available for override are `req`, `res`, `err`, and `responseTime`.
* `wrapSerializers`: when `false`, custom serializers will be passed the raw value directly. Defaults to `true`.
* `reqCustomProps`: set to a `function (req,res) => { /* returns on object */ }` or `{ /* returns on object */ }` This function will be invoked for each request with `req` and `res` where we could pass additional properties that needs to be logged outside the `req`.
* `customProps`: set to a `function (req,res) => { /* returns on object */ }` or `{ /* returns on object */ }` This function will be invoked for each request with `req` and `res` where we could pass additional properties that needs to be logged outside the `req`.
`stream`: the destination stream. Could be passed in as an option too.

#### Examples
Expand Down Expand Up @@ -169,7 +169,7 @@ var logger = require('pino-http')({
},

// Define additional custom request properties
reqCustomProps: function (req,res) {
customProps: function (req,res) {
return {
customProp: req.customProp,
// user request-scoped data is in res.locals for express applications
Expand Down
6 changes: 3 additions & 3 deletions logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function pinoLogger (opts, stream) {
var responseTimeKey = opts.customAttributeKeys.responseTime || 'responseTime'
delete opts.customAttributeKeys

var reqCustomProps = opts.reqCustomProps || {}
var customProps = opts.customProps || {}

opts.wrapSerializers = 'wrapSerializers' in opts ? opts.wrapSerializers : true
if (opts.wrapSerializers) {
Expand Down Expand Up @@ -93,8 +93,8 @@ function pinoLogger (opts, stream) {

var log = logger.child({ [reqKey]: req })

if (reqCustomProps) {
var customPropBindings = (typeof reqCustomProps === 'function') ? reqCustomProps(req, res) : reqCustomProps
if (customProps) {
var customPropBindings = (typeof customProps === 'function') ? customProps(req, res) : customProps
log = log.child(customPropBindings)
}
req.log = res.log = log
Expand Down
6 changes: 3 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ test('uses custom request properties to log additional attributes when provided'
}
}
var logger = pinoHttp({
reqCustomProps: customPropsHandler
customProps: customPropsHandler
}, dest)

setup(t, logger, function (err, server) {
Expand All @@ -788,7 +788,7 @@ test('uses custom request properties to log additional attributes when provided'
test('uses custom request properties to log additional attributes; custom props is an object instead of callback', function (t) {
var dest = split(JSON.parse)
var logger = pinoHttp({
reqCustomProps: { key1: 'value1' }
customProps: { key1: 'value1' }
}, dest)

setup(t, logger, function (err, server) {
Expand All @@ -805,7 +805,7 @@ test('uses custom request properties to log additional attributes; custom props
test('dont pass custom request properties to log additional attributes', function (t) {
var dest = split(JSON.parse)
var logger = pinoHttp({
reqCustomProps: undefined
customProps: undefined
}, dest)

setup(t, logger, function (err, server) {
Expand Down