From a10f4a29410c201eea4666af789c16226a776fdf Mon Sep 17 00:00:00 2001 From: Lam Wei Li Date: Sun, 2 Oct 2022 02:38:51 +0800 Subject: [PATCH 1/4] feat(log4js): if cb is passed to shutdown(), it must be a function or it will throw error immediately --- lib/log4js.js | 13 ++++++------- test/tap/logging-test.js | 6 ++++++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/log4js.js b/lib/log4js.js index bd2a078..502499f 100644 --- a/lib/log4js.js +++ b/lib/log4js.js @@ -98,7 +98,10 @@ function recording() { * shutdown. If an error occurs, the callback will be given the error object * as the first argument. */ -function shutdown(callback) { +function shutdown(callback = () => {}) { + if (typeof callback !== 'function') { + throw new TypeError('Invalid callback passed to shutdown'); + } debug('Shutdown called. Disabling all log writing.'); // First, disable all writing to appenders. This prevents appenders from // not being able to be drained because of run-away log writes. @@ -118,9 +121,7 @@ function shutdown(callback) { ); if (shutdownFunctions === 0) { debug('No appenders with shutdown functions found.'); - if (callback) { - callback(); - } + callback(); } let completed = 0; @@ -132,9 +133,7 @@ function shutdown(callback) { debug(`Appender shutdowns complete: ${completed} / ${shutdownFunctions}`); if (completed >= shutdownFunctions) { debug('All shutdown functions completed.'); - if (callback) { - callback(error); - } + callback(error); } } diff --git a/test/tap/logging-test.js b/test/tap/logging-test.js index 4461d14..1248b48 100644 --- a/test/tap/logging-test.js +++ b/test/tap/logging-test.js @@ -4,6 +4,12 @@ const util = require('util'); const recording = require('../../lib/appenders/recording'); test('log4js', (batch) => { + batch.test('should throw error for invalid callback to shutdown', (t) => { + const log4js = require('../../lib/log4js'); + t.throws(() => log4js.shutdown([])); + t.end(); + }); + batch.test( 'shutdown should return appenders and categories back to initial state', (t) => { From d2ef2628a71c2b3d0fd14e00f1e0dc250e6b328f Mon Sep 17 00:00:00 2001 From: Lam Wei Li Date: Sun, 2 Oct 2022 02:46:01 +0800 Subject: [PATCH 2/4] refactor(log4js): no need for `.reduceRight()`, use `.reduce()` instead --- lib/log4js.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/log4js.js b/lib/log4js.js index 502499f..593973f 100644 --- a/lib/log4js.js +++ b/lib/log4js.js @@ -115,7 +115,7 @@ function shutdown(callback = () => {}) { categories.init(); // Count the number of shutdown functions - const shutdownFunctions = appendersToCheck.reduceRight( + const shutdownFunctions = appendersToCheck.reduce( (accum, next) => (next.shutdown ? accum + 1 : accum), 0 ); From 4c12243d26d24560e5b9471ebd55e714c7b5f960 Mon Sep 17 00:00:00 2001 From: Lam Wei Li Date: Sun, 2 Oct 2022 02:51:43 +0800 Subject: [PATCH 3/4] docs: updated `api.md` for setParseCallStackFunction --- docs/api.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api.md b/docs/api.md index cedb658..5c41060 100644 --- a/docs/api.md +++ b/docs/api.md @@ -35,7 +35,7 @@ This function takes a single optional string argument to denote the category to - `addContext(,)` - where `` is a string, `` can be anything. This stores a key-value pair that is added to all log events generated by the logger. Uses would be to add ids for tracking a user through your application. Currently only the `logFaces` appenders make use of the context values. - `removeContext()` - removes a previously defined key-value pair from the context. - `clearContext()` - removes all context pairs from the logger. -- `setParseCallStackFunction(function | undefined)` - Allow to override the default way to parse the callstack data for the layout pattern, a generic javascript Error object is passed to the function. Must return an object with properties : `functionName` / `fileName` / `lineNumber` / `columnNumber` / `callStack`. Can for example be used if all of your log call are made from one "debug" class and you would to "erase" this class from the callstack to only show the function which called your "debug" class. If you pass `undefined` as the argument, it will be reset to the default parser. +- `setParseCallStackFunction(function | undefined)` - Allow to override the default way to parse the callstack data for the layout pattern, a generic javascript Error object is passed to the function. Must return an object with properties : `fileName` / `lineNumber` / `columnNumber` / `callStack` / `className` / `functionName` / `functionAlias` / `callerName`. Can, for example, be used if all of your log call are made from one "debug" class and you would to "erase" this class from the callstack to only show the function which called your "debug" class. If you pass `undefined` as the argument, it will be reset to the default parser. The `Logger` object has the following properties: From b548119f4728f1671a854e946e91164fcacc30f1 Mon Sep 17 00:00:00 2001 From: Lam Wei Li Date: Sun, 2 Oct 2022 02:54:39 +0800 Subject: [PATCH 4/4] ci: clean up --- .github/workflows/node.js.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index 488e2cb..cdb66e0 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -63,8 +63,6 @@ jobs: - name: Disable prettier on older Node.js (8.x, 10.x, 12.x) run: | sed -i '/"prettier": "prettier/d' package.json - echo Removed - sed -n '/"prettier": "prettier/p' package.json if: contains(fromJson('["8.x", "10.x", "12.x"]'), matrix.node-version) - name: Install downgraded modules ${{ matrix.npm-i }}