diff --git a/README.md b/README.md index 9c6bae0..4c14ccf 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,7 @@ $ node example.js | pino-pretty * `useLevel`: the logger level `pino-http` is using to log out the response. default: `info` * `customLogLevel`: set to a `function (req, res, err) => { /* returns level name string */ }`. This function will be invoked to determine the level at which the log should be issued (`silent` will prevent logging). This option is mutually exclusive with the `useLevel` option. The first two arguments are the HTTP request and response. The third 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. -* `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. +* `autoLogging.ignore`: set to a `function (req, res) => { /* returns boolean */ }`. Useful for defining logic based on req and res properties (such as a user-agent header or status code) to ignore 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. * `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. diff --git a/logger.js b/logger.js index 101e3b7..76e9d06 100644 --- a/logger.js +++ b/logger.js @@ -96,6 +96,11 @@ function pinoLogger (opts, stream) { let log = logger const responseTime = Date.now() - res[startTime] const req = res[reqObject] + + if (autoLoggingIgnore !== null && autoLoggingIgnore(req, res)) { + return + } + const level = getLogLevelFromCustomLogLevel(customLogLevel, useLevel, res, err, req) if (level === 'silent') { @@ -136,7 +141,7 @@ function pinoLogger (opts, stream) { } function loggingMiddleware (logger, req, res, next) { - let shouldLogSuccess = true + const shouldLogSuccess = true req.id = req.id || genReqId(req, res) @@ -179,11 +184,6 @@ function pinoLogger (opts, stream) { } if (autoLogging) { - if (autoLoggingIgnore !== null && shouldLogSuccess === true) { - const isIgnored = autoLoggingIgnore(req) - shouldLogSuccess = !isIgnored - } - if (shouldLogSuccess) { const shouldLogReceived = receivedMessage !== undefined || onRequestReceivedObject !== undefined diff --git a/test/test.js b/test/test.js index 2aeef4f..5c025a3 100644 --- a/test/test.js +++ b/test/test.js @@ -606,6 +606,31 @@ test('autoLogging set to true and path not ignored', (t, end) => { }) }) +test('autoLogging.ignore receives request and response and can filter by status code', function (t, end) { + const dest = split(JSON.parse) + const logger = pinoHttp({ + autoLogging: { + ignore: function (req, res) { + return res.statusCode === 200 + } + } + }, dest) + + setup(t, logger, function (err, server) { + assert.equal(err, undefined) + + doGet(server, '/', function () { + doGet(server, ERROR_URL) + }) + }) + + dest.on('data', function (line) { + assert.equal(line.req.url, ERROR_URL, 'Url should be the error one') + assert.equal(line.res.statusCode, 500, 'Status code should be 500') + end() + }) +}) + test('no auto logging with autoLogging set to true and ignoring a specific user-agent', function (t, end) { const dest = split(JSON.parse) const logger = pinoHttp({