mirror of
https://github.com/log4js-node/log4js-node.git
synced 2025-12-08 19:26:01 +00:00
feat: adding function(req, res) support to connectLogger options->nolog arg
This commit is contained in:
parent
3582a00747
commit
9f18c6d6b9
@ -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();
|
||||
|
||||
@ -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();
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user