Skip to content

Commit

Permalink
Fix connectLogger nolog->function with server response header
Browse files Browse the repository at this point in the history
  • Loading branch information
eyoboue committed Jul 8, 2022
1 parent b74df0f commit bfea526
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
34 changes: 21 additions & 13 deletions lib/connect-logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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();
Expand All @@ -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;
}
Expand Down
26 changes: 25 additions & 1 deletion test/tap/connect-nolog-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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();
});

Expand Down

0 comments on commit bfea526

Please sign in to comment.