diff --git a/lib/connect-logger.js b/lib/connect-logger.js index f3d3418..c8b8580 100755 --- a/lib/connect-logger.js +++ b/lib/connect-logger.js @@ -248,14 +248,18 @@ module.exports = function getLogger(logger4js, options) { const thisLogger = logger4js; let level = levels.getLevel(options.level, levels.INFO); const fmt = options.format || DEFAULT_FORMAT; - const nolog = createNoLogCondition(options.nolog); return (req, res, next) => { // mount safety if (req._logging) return next(); // nologs - if (nolog && nolog.test(req.originalUrl)) return next(); + if (typeof options.nolog === 'function') { + if (options.nolog(req, res) === true) return next(); + } else { + const nolog = createNoLogCondition(options.nolog); + if (nolog && nolog.test(req.originalUrl)) return next(); + } if (thisLogger.isLevelEnabled(level) || options.level === 'auto') { const start = new Date(); diff --git a/test/tap/connect-nolog-test.js b/test/tap/connect-nolog-test.js index 0f49d60..5d9b023 100644 --- a/test/tap/connect-nolog-test.js +++ b/test/tap/connect-nolog-test.js @@ -346,5 +346,43 @@ test('log4js connect logger', (batch) => { t.end(); }); + batch.test('nolog function', (t) => { + const ml = new MockLogger(); + const cl = clm(ml, { nolog: (_req, res) => res.statusCode < 400 }); + + t.beforeEach(() => { + ml.messages = []; + }); + + t.test('check unmatch function return (statusCode < 400)', (assert) => { + const { messages } = ml; + const req = new MockRequest('my.remote.addr', 'GET', 'http://url/log'); + const res = new MockResponse(500); + cl(req, res, () => {}); + res.end('chunk', 'encoding'); + + assert.equal(messages.length, 1); + assert.ok(levels.INFO.isEqualTo(messages[0].level)); + assert.match(messages[0].message, 'GET'); + assert.match(messages[0].message, 'http://url'); + assert.match(messages[0].message, 'my.remote.addr'); + assert.match(messages[0].message, '500'); + assert.end(); + }); + + t.test('check match function return (statusCode >= 400)', (assert) => { + const { messages } = ml; + const req = new MockRequest('my.remote.addr', 'GET', 'http://url/nolog'); + const res = new MockResponse(200); + cl(req, res, () => {}); + res.end('chunk', 'encoding'); + + assert.equal(messages.length, 0); + assert.end(); + }); + + t.end(); + }); + batch.end(); });