From f434c304e6a7acb3f9e0c437167f3abaead5d548 Mon Sep 17 00:00:00 2001 From: Jorra Singh Date: Wed, 10 Jan 2024 18:58:04 -0500 Subject: [PATCH] feat: added responseTime argument to the custom errorMessage callback --- logger.js | 2 +- test/test.js | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/logger.js b/logger.js index bca8c77..1af36ef 100644 --- a/logger.js +++ b/logger.js @@ -119,7 +119,7 @@ function pinoLogger (opts, stream) { [errKey]: error, [responseTimeKey]: responseTime }), - errorMessage(req, res, error) + errorMessage(req, res, error, responseTime) ) return diff --git a/test/test.js b/test/test.js index 9169da0..b6d4055 100644 --- a/test/test.js +++ b/test/test.js @@ -947,6 +947,26 @@ test('pass responseTime argument to the custom successMessage callback', functio }) }) +test('pass responseTime argument to the custom errorMessage callback', function (t) { + const dest = split(JSON.parse) + const customErrorMessage = 'Response time is:' + const logger = pinoHttp({ + customErrorMessage: function (req, res, err, responseTime) { + return `${customErrorMessage} ${responseTime} ${req.method}` + } + }, dest) + + setup(t, logger, function (err, server) { + t.error(err) + doGet(server, ERROR_URL) + }) + + 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) { const dest = split(JSON.parse) const logger = pinoHttp({