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 6.1.0 to 6.2.0

25

logger.js

@@ -43,3 +43,18 @@ 'use strict'

const useLevel = opts.useLevel || 'info'
const LOG_LEVEL_STRS = ['trace', 'debug', 'info', 'warn', 'error', 'fatal', 'silent']
function getValidLogLevel (level, defaultValue = 'info') {
if (level && typeof level === 'string') {
const logLevel = level.trim().toLowerCase()
if (LOG_LEVEL_STRS.includes(logLevel) === true) {
return logLevel
}
}
return defaultValue
}
function getLogLevelFromCustomLogLevel (customLogLevel, useLevel, res, err, req) {
return customLogLevel ? getValidLogLevel(customLogLevel(res, err, req), useLevel) : useLevel
}
const useLevel = getValidLogLevel(opts.useLevel)
const customLogLevel = opts.customLogLevel

@@ -58,2 +73,3 @@ delete opts.useLevel

const receivedMessage = opts.customReceivedMessage && typeof opts.customReceivedMessage === 'function' ? opts.customReceivedMessage : undefined
const successMessage = opts.customSuccessMessage || function () { return 'request completed' }

@@ -77,3 +93,3 @@ const errorMessage = opts.customErrorMessage || function () { return 'request errored' }

const responseTime = Date.now() - this[startTime]
const level = customLogLevel ? customLogLevel(this, err) : useLevel
const level = getLogLevelFromCustomLogLevel(customLogLevel, useLevel, this, err)

@@ -139,2 +155,7 @@ if (err || this.err || this.statusCode >= 500) {

if (shouldLogSuccess) {
if (receivedMessage !== undefined) {
const level = getLogLevelFromCustomLogLevel(customLogLevel, useLevel, res, undefined, req)
log[level]({}, receivedMessage(req, res))
}
res.on('finish', onResFinished)

@@ -141,0 +162,0 @@ }

4

package.json
{
"name": "pino-http",
"version": "6.1.0",
"version": "6.2.0",
"description": "High-speed HTTP logger for Node.js",

@@ -24,3 +24,3 @@ "main": "logger.js",

"ts-node": "^10.3.0",
"tsd": "^0.18.0",
"tsd": "^0.19.0",
"typescript": "^4.4.4"

@@ -27,0 +27,0 @@ },

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

* `useLevel`: the logger level `pino-http` is using to log out the response. default: `info`
* `customLogLevel`: set to a `function (res, err) => { /* returns level name string */ }`. This function will be invoked to determine the level at which the log should be issued. This option is mutually exclusive with the `useLevel` option. The first argument is the HTTP response. The second argument is an error object if an error has occurred in the request.
* `customLogLevel`: set to a `function (res, err, req) => { /* returns level name string */ }`. This function will be invoked to determine the level at which the log should be issued. This option is mutually exclusive with the `useLevel` option. The first argument is the HTTP response. The second argument is an error object if an error has occurred in the request.
* `autoLogging`: set to `false`, to disable the automatic "request completed" and "request errored" logging. Defaults to `true`. If set to an object, you can provide more options.

@@ -106,2 +106,3 @@ * `autoLogging.ignore`: set to a `function (req) => { /* returns boolean */ }`. Useful for defining logic based on req properties (such as a user-agent header) to ignore successful requests.

* `stream`: same as the second parameter
* `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.
* `customSuccessMessage`: set to a `function (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.

@@ -164,2 +165,7 @@ * `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.

// Define a custom receive message
customReceivedMessage: function (req, _res) {
return 'request received: ' + req.method
},
// Define a custom error message

@@ -166,0 +172,0 @@ customErrorMessage: function (error, res) {

@@ -13,2 +13,6 @@ 'use strict'

const DEFAULT_REQUEST_RECEIVED_MSG = 'request received'
const DEFAULT_REQUEST_COMPLETED_MSG = 'request completed'
const DEFAULT_REQUEST_ERROR_MSG = 'request errored'
function setup (t, logger, cb, handler, next) {

@@ -58,3 +62,3 @@ const server = http.createServer(handler || function (req, res) {

t.ok(line.res, 'res is defined')
t.equal(line.msg, 'request completed', 'message is set')
t.equal(line.msg, DEFAULT_REQUEST_COMPLETED_MSG, 'message is set')
t.equal(line.req.method, 'GET', 'method is get')

@@ -78,3 +82,3 @@ t.equal(line.res.statusCode, 200, 'statusCode is 200')

t.ok(line.res, 'res is defined')
t.equal(line.msg, 'request completed', 'message is set')
t.equal(line.msg, DEFAULT_REQUEST_COMPLETED_MSG, 'message is set')
t.equal(line.req.method, 'GET', 'method is get')

@@ -100,2 +104,3 @@ t.equal(line.res.statusCode, 200, 'statusCode is 200')

t.equal(options.transport.caller, join(__dirname, 'logger.js'), 'caller is set')
t.end()
})

@@ -111,2 +116,3 @@

t.equal(line.msg, 'hello world')
t.end()
})

@@ -136,3 +142,3 @@

const logger = pinoHttp({
customLogLevel: function (res, err) {
customLogLevel: function (_res, _err, _req) {
return 'warn'

@@ -154,2 +160,22 @@ }

test('uses the custom invalid log level passed in as an option', function (t) {
const dest = split(JSON.parse)
const logger = pinoHttp({
customLogLevel: function (_res, _err, _req) {
return 'error-log-level'
}
}, dest)
setup(t, logger, function (err, server) {
t.error(err)
doGet(server)
})
dest.on('data', function (line) {
t.equal(line.level, 30, 'level')
t.notOk(line.customLogLevel, 'customLogLevel not forwarded')
t.end()
})
})
test('throw error if custom log level and log level passed in together', function (t) {

@@ -160,3 +186,3 @@ const dest = split(JSON.parse)

useLevel: 'info',
customLogLevel: function (res, err) {
customLogLevel: function (_res, _err, _req) {
return 'warn'

@@ -210,2 +236,3 @@ }

t.equal(line.req.id, idToTest)
t.end()
})

@@ -233,2 +260,3 @@ })

t.equal(line.req.id, someId)
t.end()
})

@@ -287,3 +315,3 @@ })

t.ok(line.responseTime >= 0, 'responseTime is defined')
t.equal(line.msg, 'request errored', 'message is set')
t.equal(line.msg, DEFAULT_REQUEST_ERROR_MSG, 'message is set')
t.end()

@@ -474,3 +502,3 @@ })

t.ok(line.res, 'res is defined')
t.equal(line.msg, 'request completed', 'message is set')
t.equal(line.msg, DEFAULT_REQUEST_COMPLETED_MSG, 'message is set')
t.equal(line.req.method, 'GET', 'method is get')

@@ -506,3 +534,3 @@ t.equal(line.res.statusCode, 200, 'statusCode is 200')

t.notOk(line.genReqId)
t.equal(line.msg, 'request completed', 'message is set')
t.equal(line.msg, DEFAULT_REQUEST_COMPLETED_MSG, 'message is set')
t.equal(line.req.method, 'GET', 'method is get')

@@ -563,2 +591,3 @@ t.equal(line.res.statusCode, 200, 'statusCode is 200')

t.equal(Object.keys(obj.req).length, 6)
t.end()
})

@@ -576,2 +605,3 @@ })

t.equal(err.raw, error)
t.end()
}

@@ -627,2 +657,3 @@ }

t.ok(res.raw.statusCode)
t.end()
return res

@@ -653,2 +684,3 @@ }

t.equal(Object.prototype.propertyIsEnumerable.call(res, 'raw'), false)
t.end()
return res

@@ -718,2 +750,3 @@ }

t.equal(typeof req.id === 'function', false)
t.end()
return req

@@ -756,2 +789,47 @@ }

test('uses the custom receivedMessage callback if passed in as an option', function (t) {
const dest = split(JSON.parse)
const message = DEFAULT_REQUEST_RECEIVED_MSG
const logger = pinoHttp({
customReceivedMessage: function (_req, _res) {
return message
}
}, dest)
setup(t, logger, function (err, server) {
t.error(err)
doGet(server)
})
dest.on('data', function (line) {
if (line.msg === DEFAULT_REQUEST_COMPLETED_MSG) {
return
}
t.equal(line.msg, message)
t.end()
})
})
test('receve receivedMessage before successMessage', function (t) {
t.plan(3)
const dest = split(JSON.parse)
const message = DEFAULT_REQUEST_RECEIVED_MSG
const logger = pinoHttp({
customReceivedMessage: function (_req, _res) {
return message
}
}, dest)
setup(t, logger, function (err, server) {
t.error(err)
doGet(server, null, function () {
t.equal(dest.read().msg, DEFAULT_REQUEST_RECEIVED_MSG)
t.equal(dest.read().msg, DEFAULT_REQUEST_COMPLETED_MSG)
t.end()
})
})
})
test('uses the custom errorMessage callback if passed in as an option', function (t) {

@@ -777,2 +855,24 @@ const dest = split(JSON.parse)

test('receve receivedMessage before errorMessage', function (t) {
t.plan(3)
const dest = split(JSON.parse)
const message = DEFAULT_REQUEST_RECEIVED_MSG
const logger = pinoHttp({
customReceivedMessage: function (_req, _res) {
return message
}
}, dest)
setup(t, logger, function (err, server) {
t.error(err)
doGet(server, ERROR_URL, function () {
t.equal(dest.read().msg, DEFAULT_REQUEST_RECEIVED_MSG)
t.equal(dest.read().msg, DEFAULT_REQUEST_ERROR_MSG)
t.end()
})
})
})
test('uses custom log object attribute keys when provided, successful request', function (t) {

@@ -928,3 +1028,5 @@ const dest = split(JSON.parse)

const line = dest.read()
t.equal(line.msg, 'request completed')
t.equal(line.msg, DEFAULT_REQUEST_COMPLETED_MSG)
t.end()
})

@@ -961,5 +1063,7 @@ }, function (req, res) {

const responseLine = dest.read()
t.equal(responseLine.msg, 'request completed')
t.equal(responseLine.msg, DEFAULT_REQUEST_COMPLETED_MSG)
t.equal(responseLine.reqId, 'testId')
t.ok(responseLine.req)
t.end()
})

@@ -991,7 +1095,9 @@ }, handler)

const responseLine = dest.read()
t.equal(responseLine.msg, 'request completed')
t.equal(responseLine.msg, DEFAULT_REQUEST_COMPLETED_MSG)
t.equal(responseLine.customRequestId, 'testId')
t.ok(responseLine.req)
t.end()
})
}, handler)
})
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