From c3d32dd87860e731411a79a49bae0ea212e7e81f Mon Sep 17 00:00:00 2001 From: jorra-singh <106191642+jorra-singh@users.noreply.github.com> Date: Mon, 6 May 2024 17:38:15 -0400 Subject: [PATCH] feat: added responseTime argument to the custom errorMessage callback (#320) --- 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 0469c83..448a809 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({