Comparing version 8.2.1 to 8.3.0
@@ -9,3 +9,3 @@ import { expectType } from 'tsd'; | ||
const pinoHttpCjs = require('.'); | ||
const { pinoHttp: pinoHttpCjsNamed } = require('.') | ||
const { pinoHttp: pinoHttpCjsNamed } = require('.'); | ||
@@ -12,0 +12,0 @@ const logger = pino(); |
@@ -83,2 +83,3 @@ // Project: https://github.com/pinojs/pino-http#readme | ||
log: pino.Logger; | ||
allLogs: pino.Logger[]; | ||
} | ||
@@ -85,0 +86,0 @@ |
@@ -21,3 +21,2 @@ | ||
pinoHttp({ useLevel: 'error' }); | ||
pinoHttp({ prettyPrint: true }); // deprecated but still present in pino. | ||
pinoHttp({ transport: { target: 'pino-pretty', options: { colorize: true } } }); | ||
@@ -161,2 +160,3 @@ pinoHttp({ autoLogging: false }); | ||
request.log.info(`Request received with request ID ${request.id}`); | ||
request.allLogs[0].info("Request Received"); | ||
// res[startTime] should be available | ||
@@ -163,0 +163,0 @@ response[startTime] = Date.now(); |
@@ -40,3 +40,3 @@ 'use strict' | ||
if (opts.useLevel && opts.customLogLevel) { | ||
throw new Error("You can't pass 'useLevel' and 'customLogLevel' together") | ||
throw new Error('You can\'t pass \'useLevel\' and \'customLogLevel\' together') | ||
} | ||
@@ -89,14 +89,13 @@ | ||
const genReqId = reqIdGenFactory(opts.genReqId) | ||
loggingMiddleware.logger = logger | ||
return loggingMiddleware | ||
const result = (req, res, next) => { | ||
return loggingMiddleware(logger, req, res, next) | ||
} | ||
result.logger = logger | ||
return result | ||
function onResFinished (err) { | ||
this.removeListener('close', onResFinished) | ||
this.removeListener('error', onResFinished) | ||
this.removeListener('finish', onResFinished) | ||
function onResFinished (res, logger, err) { | ||
let log = logger | ||
const responseTime = Date.now() - res[startTime] | ||
const level = getLogLevelFromCustomLogLevel(customLogLevel, useLevel, res, err) | ||
let log = this.log | ||
const responseTime = Date.now() - this[startTime] | ||
const level = getLogLevelFromCustomLogLevel(customLogLevel, useLevel, this, err) | ||
if (level === 'silent') { | ||
@@ -106,20 +105,19 @@ return | ||
const req = this[reqObject] | ||
const res = this | ||
const req = res[reqObject] | ||
const customPropBindings = (typeof customProps === 'function') ? customProps(req, res) : customProps | ||
if (customPropBindings) { | ||
log = this.log.child(customPropBindings) | ||
log = logger.child(customPropBindings) | ||
} | ||
if (err || this.err || this.statusCode >= 500) { | ||
const error = err || this.err || new Error('failed with status code ' + this.statusCode) | ||
if (err || res.err || res.statusCode >= 500) { | ||
const error = err || res.err || new Error('failed with status code ' + res.statusCode) | ||
log[level]( | ||
onRequestErrorObject(req, this, error, { | ||
[resKey]: this, | ||
onRequestErrorObject(req, res, error, { | ||
[resKey]: res, | ||
[errKey]: error, | ||
[responseTimeKey]: responseTime | ||
}), | ||
errorMessage(req, this, error) | ||
errorMessage(req, res, error) | ||
) | ||
@@ -131,14 +129,14 @@ | ||
log[level]( | ||
onRequestSuccessObject(req, this, { | ||
[resKey]: this, | ||
onRequestSuccessObject(req, res, { | ||
[resKey]: res, | ||
[responseTimeKey]: responseTime | ||
}), | ||
successMessage(req, this) | ||
successMessage(req, res, responseTime) | ||
) | ||
} | ||
function loggingMiddleware (req, res, next) { | ||
function loggingMiddleware (logger, req, res, next) { | ||
let shouldLogSuccess = true | ||
req.id = genReqId(req, res) | ||
req.id = req.id || genReqId(req, res) | ||
@@ -153,5 +151,21 @@ const log = quietReqLogger ? logger.child({ [requestIdKey]: req.id }) : logger | ||
res.log = fullReqLogger | ||
req.log = quietReqLogger ? log : fullReqLogger | ||
const responseLogger = fullReqLogger | ||
const requestLogger = quietReqLogger ? log : fullReqLogger | ||
if (!res.log) { | ||
res.log = responseLogger | ||
} | ||
if (!res.allLogs) { | ||
res.allLogs = [] | ||
} | ||
res.allLogs.push(responseLogger) | ||
if (!req.log) { | ||
req.log = requestLogger | ||
} | ||
if (!req.allLogs) { | ||
req.allLogs = [] | ||
} | ||
req.allLogs.push(requestLogger) | ||
res[startTime] = res[startTime] || Date.now() | ||
@@ -161,2 +175,9 @@ // carry request to be executed when response is finished | ||
const onResponseComplete = (err) => { | ||
res.removeListener('close', onResponseComplete) | ||
res.removeListener('finish', onResponseComplete) | ||
res.removeListener('error', onResponseComplete) | ||
return onResFinished(res, responseLogger, err) | ||
} | ||
if (autoLogging) { | ||
@@ -176,10 +197,10 @@ if (autoLoggingIgnore !== null && shouldLogSuccess === true) { | ||
req.log[level](receivedObjectResult, receivedStringResult) | ||
requestLogger[level](receivedObjectResult, receivedStringResult) | ||
} | ||
res.on('close', onResFinished) | ||
res.on('finish', onResFinished) | ||
res.on('close', onResponseComplete) | ||
res.on('finish', onResponseComplete) | ||
} | ||
res.on('error', onResFinished) | ||
res.on('error', onResponseComplete) | ||
} | ||
@@ -186,0 +207,0 @@ |
{ | ||
"name": "pino-http", | ||
"version": "8.2.1", | ||
"version": "8.3.0", | ||
"description": "High-speed HTTP logger for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "logger.js", |
@@ -121,2 +121,46 @@ 'use strict' | ||
test('internal pino logger not shared between multiple middleware', function (t) { | ||
t.plan(1) | ||
const dest = split(JSON.parse) | ||
const middleware1 = pinoHttp(dest) | ||
const middleware2 = pinoHttp(dest) | ||
t.not(middleware1.logger, middleware2.logger, 'expected loggers not to be shared between middleware invocations') | ||
}) | ||
test('when multiple pino middleware are present each pino logger retains its own redact config', function (t) { | ||
t.plan(6) | ||
const middleware1Output = split(JSON.parse) | ||
const middleware2Output = split(JSON.parse) | ||
const middleware3Output = split(JSON.parse) | ||
const middleware1 = pinoHttp({ redact: ['req.method'] }, middleware1Output) | ||
const middleware2 = pinoHttp({ redact: ['req.url'] }, middleware2Output) | ||
const middleware3 = pinoHttp({}, middleware3Output) | ||
setup(t, (req, res, next) => { | ||
middleware1(req, res, next) | ||
middleware2(req, res, next) | ||
middleware3(req, res, next) | ||
t.ok(req.log, 'pino http middleware should have set request log logger to middleware1\'s logger') | ||
t.equal(req.allLogs.length, 3, 'multiple pino http middleware should have set request additional loggers') | ||
}, function (err, server) { | ||
t.error(err) | ||
doGet(server, '/') | ||
}) | ||
middleware1Output.on('data', function (line) { | ||
t.equal(line.req.method, '[Redacted]', 'method is Redacted') | ||
}) | ||
middleware2Output.on('data', function (line) { | ||
t.equal(line.req.url, '[Redacted]', 'url is Redacted') | ||
}) | ||
middleware3Output.on('data', function (line) { | ||
t.equal(line.req.method, 'GET', 'method is get and not redacted') | ||
}) | ||
}) | ||
test('uses the log level passed in as an option', function (t) { | ||
@@ -265,3 +309,3 @@ const dest = split(JSON.parse) | ||
} | ||
t.throws(throwFunction, { message: "You can't pass 'useLevel' and 'customLogLevel' together" }) | ||
t.throws(throwFunction, { message: 'You can\'t pass \'useLevel\' and \'customLogLevel\' together' }) | ||
t.end() | ||
@@ -295,2 +339,3 @@ }) | ||
let idToTest | ||
function genReqId (req, res) { | ||
@@ -543,2 +588,3 @@ t.ok(res, 'res is defined') | ||
let idToTest | ||
function genReqId (req, res) { | ||
@@ -838,2 +884,22 @@ t.ok(res, 'res is defined') | ||
test('pass responseTime argument to the custom successMessage callback', function (t) { | ||
const dest = split(JSON.parse) | ||
const customResponseMessage = 'Response time is: ' | ||
const logger = pinoHttp({ | ||
customSuccessMessage: function (req, res, responseTime) { | ||
return customResponseMessage + responseTime + ' ' + req.method | ||
} | ||
}, dest) | ||
setup(t, logger, function (err, server) { | ||
t.error(err) | ||
doGet(server) | ||
}) | ||
dest.on('data', function (line) { | ||
t.match(line.msg, /Response time is: \d+ GET/) | ||
t.end() | ||
}) | ||
}) | ||
test('uses the custom successObject callback if passed in as an option', function (t) { | ||
@@ -1066,2 +1132,3 @@ const dest = split(JSON.parse) | ||
const dest = split(JSON.parse) | ||
function customPropsHandler (req, res) { | ||
@@ -1075,2 +1142,3 @@ if (req && res) { | ||
} | ||
const logger = pinoHttp({ | ||
@@ -1094,2 +1162,3 @@ customProps: customPropsHandler | ||
const dest = split(JSON.parse) | ||
function customPropsHandler (req, res) { | ||
@@ -1103,2 +1172,3 @@ if (req && res) { | ||
} | ||
const logger = pinoHttp({ | ||
@@ -1122,2 +1192,3 @@ customProps: customPropsHandler | ||
const dest = split(JSON.parse) | ||
function customPropsHandler (req, res) { | ||
@@ -1131,2 +1202,3 @@ if (req && res) { | ||
} | ||
const logger = pinoHttp({ | ||
@@ -1133,0 +1205,0 @@ customProps: customPropsHandler |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
75159
1761