diff --git a/lib/connect-logger.js b/lib/connect-logger.js index bb5ac87c..6631c8c3 100755 --- a/lib/connect-logger.js +++ b/lib/connect-logger.js @@ -251,12 +251,10 @@ module.exports = function getLogger(logger4js, options) { return (req, res, next) => { // mount safety - if (req._logging) return next(); + if (req._logging !== undefined) return next(); // nologs - if (typeof options.nolog === 'function') { - if (options.nolog(req, res) === true) return next(); - } else { + if (typeof options.nolog !== 'function') { const nolog = createNoLogCondition(options.nolog); if (nolog && nolog.test(req.originalUrl)) return next(); } @@ -284,6 +282,15 @@ module.exports = function getLogger(logger4js, options) { return; } finished = true; + + // nologs + if (typeof options.nolog === 'function') { + if (options.nolog(req, res) === true) { + req._logging = false; + return; + } + } + res.responseTime = new Date() - start; // status code response level handling if (res.statusCode && options.level === 'auto') { diff --git a/test/tap/configuration-validation-test.js b/test/tap/configuration-validation-test.js index 401b5f97..cd0ad06c 100644 --- a/test/tap/configuration-validation-test.js +++ b/test/tap/configuration-validation-test.js @@ -274,7 +274,9 @@ test('log4js configuration validation', (batch) => { }) ) ); - t.end(); + log4js.shutdown(() => { + t.end(); + }); }); batch.test('should load appenders from core first', (t) => { 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(); });