diff --git a/lib/connect-logger.js b/lib/connect-logger.js index 07d07e31..701d3c68 100755 --- a/lib/connect-logger.js +++ b/lib/connect-logger.js @@ -208,6 +208,25 @@ function matchRules(statusCode, currentLevel, ruleSet) { return level; } +/** + * Log skipping checker + * + * @param {IncomingMessage} req + * @param {ServerResponse} res + * @param {Array|string|RegExp|Function} nolog + * @return {boolean} + * @api private + */ +function shouldSkip(req, res, nolog) { + if (typeof nolog === 'function') { + if (nolog(req, res) === true) return true; + } else { + const regexp = createNoLogCondition(nolog); + if (regexp && regexp.test(req.originalUrl)) return true; + } + return false; +} + /** * Log requests with the given `options` or a `format` string. * @@ -253,19 +272,8 @@ module.exports = function getLogger(logger4js, options) { // mount safety if (req._logging) return next(); - // Create log skipping checker (nologs) - const shouldSkip = (_req, _res) => { - if (typeof options.nolog === 'function') { - if (options.nolog(_req, _res) === true) return true; - } else { - const nolog = createNoLogCondition(options.nolog); - if (nolog && nolog.test(_req.originalUrl)) return true; - } - return false; - }; - // nologs - if (shouldSkip(req, res)) next(); + if (shouldSkip(req, res, options.nolog)) return next(); if (thisLogger.isLevelEnabled(level) || options.level === 'auto') { const start = new Date(); @@ -292,7 +300,7 @@ module.exports = function getLogger(logger4js, options) { finished = true; // nologs - if (shouldSkip(req, res)) { + if (shouldSkip(req, res, options.nolog)) { req._logging = false; return; } diff --git a/test/tap/connect-nolog-test.js b/test/tap/connect-nolog-test.js index 5d9b0232..a0b1e30d 100644 --- a/test/tap/connect-nolog-test.js +++ b/test/tap/connect-nolog-test.js @@ -348,7 +348,10 @@ test('log4js connect logger', (batch) => { batch.test('nolog function', (t) => { const ml = new MockLogger(); - const cl = clm(ml, { nolog: (_req, res) => res.statusCode < 400 }); + const cl = clm(ml, { + nolog: (_req, res) => + res.getHeader('content-type') === 'image/png' || res.statusCode < 400, + }); t.beforeEach(() => { ml.messages = []; @@ -381,6 +384,27 @@ test('log4js connect logger', (batch) => { assert.end(); }); + t.test( + 'check match function server response content-type header', + (assert) => { + const { messages } = ml; + const req = new MockRequest( + 'my.remote.addr', + 'GET', + 'http://url/nolog' + ); + const res = new MockResponse(500); + res.on('finish', () => { + res.setHeader('content-type', 'image/png'); + }); + cl(req, res, () => {}); + res.end('chunk', 'encoding'); + + assert.equal(messages.length, 0); + assert.end(); + } + ); + t.end(); });