Skip to content

Commit 7ca308d

Browse files
authored
Merge pull request #1334 from log4js-node/feat/invalid-shutdown-callback
feat(log4js): if cb is passed to `shutdown()`, it must be a function or it will throw error immediately
2 parents cfbc7a0 + b548119 commit 7ca308d

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

.github/workflows/node.js.yml

-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ jobs:
6363
- name: Disable prettier on older Node.js (8.x, 10.x, 12.x)
6464
run: |
6565
sed -i '/"prettier": "prettier/d' package.json
66-
echo Removed
67-
sed -n '/"prettier": "prettier/p' package.json
6866
if: contains(fromJson('["8.x", "10.x", "12.x"]'), matrix.node-version)
6967

7068
- name: Install downgraded modules ${{ matrix.npm-i }}

docs/api.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ This function takes a single optional string argument to denote the category to
3535
- `addContext(<key>,<value>)` - where `<key>` is a string, `<value>` 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.
3636
- `removeContext(<key>)` - removes a previously defined key-value pair from the context.
3737
- `clearContext()` - removes all context pairs from the logger.
38-
- `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.
38+
- `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.
3939

4040
The `Logger` object has the following properties:
4141

lib/log4js.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ function recording() {
9898
* shutdown. If an error occurs, the callback will be given the error object
9999
* as the first argument.
100100
*/
101-
function shutdown(callback) {
101+
function shutdown(callback = () => {}) {
102+
if (typeof callback !== 'function') {
103+
throw new TypeError('Invalid callback passed to shutdown');
104+
}
102105
debug('Shutdown called. Disabling all log writing.');
103106
// First, disable all writing to appenders. This prevents appenders from
104107
// not being able to be drained because of run-away log writes.
@@ -112,15 +115,13 @@ function shutdown(callback) {
112115
categories.init();
113116

114117
// Count the number of shutdown functions
115-
const shutdownFunctions = appendersToCheck.reduceRight(
118+
const shutdownFunctions = appendersToCheck.reduce(
116119
(accum, next) => (next.shutdown ? accum + 1 : accum),
117120
0
118121
);
119122
if (shutdownFunctions === 0) {
120123
debug('No appenders with shutdown functions found.');
121-
if (callback) {
122-
callback();
123-
}
124+
callback();
124125
}
125126

126127
let completed = 0;
@@ -132,9 +133,7 @@ function shutdown(callback) {
132133
debug(`Appender shutdowns complete: ${completed} / ${shutdownFunctions}`);
133134
if (completed >= shutdownFunctions) {
134135
debug('All shutdown functions completed.');
135-
if (callback) {
136-
callback(error);
137-
}
136+
callback(error);
138137
}
139138
}
140139

test/tap/logging-test.js

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ const util = require('util');
44
const recording = require('../../lib/appenders/recording');
55

66
test('log4js', (batch) => {
7+
batch.test('should throw error for invalid callback to shutdown', (t) => {
8+
const log4js = require('../../lib/log4js');
9+
t.throws(() => log4js.shutdown([]));
10+
t.end();
11+
});
12+
713
batch.test(
814
'shutdown should return appenders and categories back to initial state',
915
(t) => {

0 commit comments

Comments
 (0)