Merge pull request #661 from barwin/barwin/redis-appender-shutdown

feat(redis): quit on log4js.shutdown
This commit is contained in:
Gareth Jones 2018-02-12 08:58:02 +11:00 committed by GitHub
commit 82d060247f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 2 deletions

View File

@ -15,7 +15,7 @@ function redisAppender(config, layout) {
}
});
return function (loggingEvent) {
const appender = function (loggingEvent) {
const message = layout(loggingEvent);
redisClient.publish(config.channel, message, (err) => {
if (err) {
@ -23,6 +23,13 @@ function redisAppender(config, layout) {
}
});
};
appender.shutdown = (cb) => {
redisClient.quit();
if (cb) cb();
};
return appender;
}
function configure(config, layouts) {

View File

@ -19,7 +19,10 @@ function setupLogging(category, options) {
publish: function (channel, message, callback) {
fakeRedis.msgs.push({ channel: channel, message: message });
fakeRedis.publishCb = callback;
}
},
quit: function () {
fakeRedis.quitCalled = true;
},
};
}
};
@ -46,6 +49,7 @@ function setupLogging(category, options) {
return {
logger: log4js.getLogger(category),
log4js: log4js,
fakeRedis: fakeRedis,
fakeConsole: fakeConsole
};
@ -129,5 +133,14 @@ test('log4js redisAppender', (batch) => {
t.end();
});
batch.test('shutdown', (t) => {
const setup = setupLogging('shutdown', { type: 'redis', channel: 'testing' });
setup.log4js.shutdown(() => {
t.ok(setup.fakeRedis.quitCalled);
t.end();
});
});
batch.end();
});