Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 6 additions & 6 deletions logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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

Expand Down
25 changes: 25 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down