Socket
Socket
Sign inDemoInstall

pino-http

Package Overview
Dependencies
Maintainers
4
Versions
80
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pino-http - npm Package Compare versions

Comparing version 8.0.1 to 8.1.0

3

index.d.ts

@@ -35,2 +35,5 @@ // Project: https://github.com/pinojs/pino-http#readme

customErrorMessage?: ((req: IncomingMessage, res: ServerResponse, error: Error) => string) | undefined;
customReceivedObject?: ((req: IncomingMessage, res: ServerResponse, val?: any) => any) | undefined;
customSuccessObject?: ((req: IncomingMessage, res: ServerResponse, val: any) => any) | undefined;
customErrorObject?: ((req: IncomingMessage, res: ServerResponse, error: Error, val: any) => string) | undefined;
customAttributeKeys?: CustomAttributeKeys | undefined;

@@ -37,0 +40,0 @@ wrapSerializers?: boolean | undefined;

70

logger.js

@@ -67,5 +67,11 @@ 'use strict'

const receivedMessage = opts.customReceivedMessage && typeof opts.customReceivedMessage === 'function' ? opts.customReceivedMessage : undefined
const successMessage = opts.customSuccessMessage || function (req, res) { return res.writableEnded ? 'request completed' : 'request aborted' }
const errorMessage = opts.customErrorMessage || function () { return 'request errored' }
const onRequestReceivedObject = getFunctionOrDefault(opts.customReceivedObject, undefined)
const receivedMessage = getFunctionOrDefault(opts.customReceivedMessage, undefined)
const onRequestSuccessObject = getFunctionOrDefault(opts.customSuccessObject, defaultSuccessfulRequestObjectProvider)
const successMessage = getFunctionOrDefault(opts.customSuccessMessage, defaultSuccessfulRequestMessageProvider)
const onRequestErrorObject = getFunctionOrDefault(opts.customErrorObject, defaultFailedRequestObjectProvider)
const errorMessage = getFunctionOrDefault(opts.customErrorMessage, defaultFailedRequestMessageProvider)
delete opts.customSuccessfulMessage

@@ -110,14 +116,21 @@ delete opts.customErroredMessage

log[level]({
[resKey]: this,
[errKey]: error,
[responseTimeKey]: responseTime
}, errorMessage(req, this, error))
log[level](
onRequestErrorObject(req, this, error, {
[resKey]: this,
[errKey]: error,
[responseTimeKey]: responseTime
}),
errorMessage(req, this, error)
)
return
}
log[level]({
[resKey]: this,
[responseTimeKey]: responseTime
}, successMessage(req, this))
log[level](
onRequestSuccessObject(req, this, {
[resKey]: this,
[responseTimeKey]: responseTime
}),
successMessage(req, this)
)
}

@@ -152,5 +165,10 @@

if (shouldLogSuccess) {
if (receivedMessage !== undefined) {
const shouldLogReceived = receivedMessage !== undefined || onRequestReceivedObject !== undefined
if (shouldLogReceived) {
const level = getLogLevelFromCustomLogLevel(customLogLevel, useLevel, res, undefined, req)
req.log[level](receivedMessage(req, res))
const receivedObjectResult = onRequestReceivedObject !== undefined ? onRequestReceivedObject(req, res, undefined) : {}
const receivedStringResult = receivedMessage !== undefined ? receivedMessage(req, res) : undefined
req.log[level](receivedObjectResult, receivedStringResult)
}

@@ -202,2 +220,26 @@

function getFunctionOrDefault (value, defaultValue) {
if (value && typeof value === 'function') {
return value
}
return defaultValue
}
function defaultSuccessfulRequestObjectProvider (req, res, successObject) {
return successObject
}
function defaultFailedRequestObjectProvider (req, res, error, errorObject) {
return errorObject
}
function defaultFailedRequestMessageProvider () {
return 'request errored'
}
function defaultSuccessfulRequestMessageProvider (req, res) {
return res.writableEnded ? 'request completed' : 'request aborted'
}
module.exports = pinoLogger

@@ -204,0 +246,0 @@ module.exports.stdSerializers = {

{
"name": "pino-http",
"version": "8.0.1",
"version": "8.1.0",
"description": "High-speed HTTP logger for Node.js",

@@ -11,7 +11,7 @@ "main": "logger.js",

"pino": "^8.0.0",
"pino-std-serializers": "^5.0.0",
"pino-std-serializers": "^6.0.0",
"process-warning": "^2.0.0"
},
"devDependencies": {
"@types/node": "^17.0.0",
"@types/node": "^18.0.0",
"autocannon": "^7.3.0",

@@ -18,0 +18,0 @@ "coveralls": "^3.0.0",

@@ -104,4 +104,7 @@ # pino-http  [![Build Status](https://img.shields.io/github/workflow/status/pinojs/pino-http/CI)](https://github.com/pinojs/pino-http/actions)

* `customReceivedMessage`: set to a `function (req, res) => { /* returns message string */ }` This function will be invoked at each request received, setting "msg" property to returned string. If not set, nothing value will be used.
* `customReceivedObject`: set to a `function (req, res, loggableObject) => { /* returns loggable object */ }` This function will be invoked at each request received, replacing the base loggable received object. When set, it is up to the reponsibility of the caller to merge with the `loggableObject` parameter. If not set, default value will be used.
* `customSuccessMessage`: set to a `function (req, res) => { /* returns message string */ }` This function will be invoked at each successful response, setting "msg" property to returned string. If not set, default value will be used.
* `customSuccessObject`: set to a `function (req, res, loggableObject) => { /* returns loggable object */ }` This function will be invoked at each successful response, replacing the base loggable success object. When set, it is up to the reponsibility of the caller to merge with the `loggableObject` parameter. If not set, default value will be used.
* `customErrorMessage`: set to a `function (req, res, err) => { /* 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.
* `customErrorObject`: set to a `function (req, res, err, loggableObject) => { /* returns message string */ }` This function will be invoked at each failed response, the base loggable error object. When set, it is up to the reponsibility of the caller to merge with the `loggableObject` parameter. 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`, `responseTime` and, when using quietReqLogger, `reqId`.

@@ -201,2 +204,49 @@ * `wrapSerializers`: when `false`, custom serializers will be passed the raw value directly. Defaults to `true`.

##### Structured Object Hooks
It is possible to override the default structured object with your own. The hook is provided with the
pino-http base object so that you can merge in your own keys.
This is useful in scenarios where you want to augment core pino-http logger object with your own event
labels.
> If you simply want to change the message which is logged then check out the custom[Received|Error|Success]Message
> hooks e.g. customReceivedMessage
```js
const logger = require('pino-http')({
//... remaining config omitted for brevity
customReceivedObject: (req, res, val) => {
return {
category: 'ApplicationEvent',
eventCode: 'REQUEST_RECEIVED'
};
},
customSuccessObject: (req, res, val) => {
return {
...val,
category: 'ApplicationEvent',
eventCode:
res.statusCode < 300
? 'REQUEST_PROCESSED'
: 'REQUEST_FAILED'
};
},
customErrorObject: (req, res, error, val) => {
const store = storage.getStore();
const formattedBaggage = convertBaggageToObject(store?.baggage);
return {
...val,
category: 'ApplicationEvent',
eventCode: 'REQUEST_FAILED'
};
}
// ...remaining config omitted for brevity
})
```
##### PinoHttp.logger (P.Logger)

@@ -203,0 +253,0 @@

@@ -832,2 +832,21 @@ 'use strict'

test('uses the custom successObject callback if passed in as an option', function (t) {
const dest = split(JSON.parse)
const logger = pinoHttp({
customSuccessObject: function (req, res, val) {
return { ...val, label: req.method + ' customSuccessObject' }
}
}, dest)
setup(t, logger, function (err, server) {
t.error(err)
doGet(server)
})
dest.on('data', function (line) {
t.equal(line.label, 'GET customSuccessObject')
t.end()
})
})
test('uses the custom receivedMessage callback if passed in as an option', function (t) {

@@ -856,2 +875,53 @@ const dest = split(JSON.parse)

test('uses the custom receivedObject callback if passed in as an option', function (t) {
const dest = split(JSON.parse)
const logger = pinoHttp({
customReceivedObject: function (req, val) {
return { label: req.method + ' customReceivedObject' }
}
}, dest)
setup(t, logger, function (err, server) {
t.error(err)
doGet(server)
})
dest.on('data', function (line) {
if (line.label === undefined) {
return
}
t.equal(line.label, 'GET customReceivedObject')
t.end()
})
})
test('uses the custom receivedObject + receivedMessage callback if passed in as an option', function (t) {
const dest = split(JSON.parse)
const logger = pinoHttp({
customReceivedMessage: function (_req, _res) {
return DEFAULT_REQUEST_RECEIVED_MSG
},
customReceivedObject: function (req, val) {
return { label: req.method + ' customReceivedObject' }
}
}, dest)
setup(t, logger, function (err, server) {
t.error(err)
doGet(server)
})
dest.on('data', function (line) {
if (line.label === undefined && line.msg !== undefined) {
return
}
t.equal(line.msg, DEFAULT_REQUEST_RECEIVED_MSG)
t.equal(line.label, 'GET customReceivedObject')
t.end()
})
})
test('receve receivedMessage before successMessage', function (t) {

@@ -899,2 +969,21 @@ t.plan(3)

test('uses the custom errorObject callback if passed in as an option', function (t) {
const dest = split(JSON.parse)
const logger = pinoHttp({
customErrorObject: function (req, res, err, val) {
return { ...val, label: 'customErrorObject ' + req.method + ' ' + err.toString() }
}
}, dest)
setup(t, logger, function (err, server) {
t.error(err)
doGet(server, ERROR_URL)
})
dest.on('data', function (line) {
t.equal(line.label.indexOf('customErrorObject GET'), 0)
t.end()
})
})
test('receve receivedMessage before errorMessage', function (t) {

@@ -901,0 +990,0 @@ t.plan(3)

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc