From 9f18c6d6b9f0c09d9020ea1d9ff7cfb9fbd7e2af Mon Sep 17 00:00:00 2001 From: Eugene YOBOUE Date: Sun, 3 Jul 2022 13:32:41 +0000 Subject: [PATCH 1/2] feat: adding function(req, res) support to connectLogger options->nolog arg --- lib/connect-logger.js | 8 +++++-- test/tap/connect-nolog-test.js | 38 ++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/lib/connect-logger.js b/lib/connect-logger.js index f3d3418c..c8b85800 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 0f49d606..5d9b0232 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(); }); From 3279647175c95b160208ed8a572516da89cdb982 Mon Sep 17 00:00:00 2001 From: Eugene YOBOUE Date: Sun, 3 Jul 2022 19:47:47 +0000 Subject: [PATCH 2/2] chore(feat): Update #1279 docs --- docs/connect-logger.md | 16 ++++++++++++++-- lib/connect-logger.js | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/docs/connect-logger.md b/docs/connect-logger.md index e63e420a..6dd59398 100644 --- a/docs/connect-logger.md +++ b/docs/connect-logger.md @@ -30,7 +30,7 @@ The log4js.connectLogger supports the passing of an options object that can be u - log level - log format string or function (the same as the connect/express logger) -- nolog expressions (represented as a string, regexp, or array) +- nolog expressions (represented as a string, regexp, array, or function(req, res)) - status code rulesets For example: @@ -97,7 +97,7 @@ app.use( ); ``` -The log4js.connectLogger also supports a nolog option where you can specify a string, regexp, or array to omit certain log messages. Example of 1.2 below. +The log4js.connectLogger also supports a nolog option where you can specify a string, regexp, array, or function(req, res) to omit certain log messages. Example of 1.2 below. ```javascript app.use( @@ -109,6 +109,18 @@ app.use( ); ``` +or + +```javascript +app.use( + log4js.connectLogger(logger, { + level: "auto", + format: ":method :url", + nolog: (req, res) => res.statusCode < 400, + }) +); +``` + The log4js.connectLogger can add a response of express to context if `context` flag is set to `true`. Application can use it in layouts or appenders. diff --git a/lib/connect-logger.js b/lib/connect-logger.js index c8b85800..bb5ac87c 100755 --- a/lib/connect-logger.js +++ b/lib/connect-logger.js @@ -215,7 +215,7 @@ function matchRules(statusCode, currentLevel, ruleSet) { * * - `format` Format string, see below for tokens * - `level` A log4js levels instance. Supports also 'auto' - * - `nolog` A string or RegExp to exclude target logs + * - `nolog` A string or RegExp to exclude target logs or function(req, res): boolean * - `statusRules` A array of rules for setting specific logging levels base on status codes * - `context` Whether to add a response of express to the context *