Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle double shutdown() #1083

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/log4js.js
Expand Up @@ -82,6 +82,13 @@ function configure(configurationFileOrObject) {
* as the first argument.
*/
function shutdown(cb) {
if (!enabled) {
if (cb) {
cb(undefined);
}
return null;
}

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.
Expand Down
26 changes: 26 additions & 0 deletions test/tap/fileAppender-test.js
Expand Up @@ -376,5 +376,31 @@ test("log4js fileAppender", batch => {
t.end();
});

batch.test("with double shutdown", async t => {
const testFile = path.join(__dirname, "fa-default-test.log");
await removeFile(testFile);

log4js.configure({
appenders: { test: { type: "file", filename: testFile } },
categories: { default: { appenders: ["test"], level: "trace" } }
});
const logger = log4js.getLogger("default-settings");

logger.info("1");
logger.info("2");
logger.info("3");

await new Promise(resolve => log4js.shutdown(resolve));
await new Promise(resolve => log4js.shutdown(resolve));
const fileContents = await fs.readFile(testFile, "utf8");
// 3 lines of output, plus the trailing newline.
t.equal(fileContents.split(EOL).length, 4);
t.match(
fileContents,
/\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}] \[INFO] default-settings - /
);
t.end();
});

batch.end();
});