mirror of
https://github.com/log4js-node/log4js-node.git
synced 2025-12-08 19:26:01 +00:00
added passing shutdown function to addAppender, also added enableLogWrites call to clearAppenders
This commit is contained in:
commit
99571a5c9a
@ -2,8 +2,9 @@
|
||||
var layouts = require('../layouts')
|
||||
, loggly = require('loggly')
|
||||
, os = require('os')
|
||||
, passThrough = layouts.messagePassThroughLayout;
|
||||
|
||||
, passThrough = layouts.messagePassThroughLayout
|
||||
, openRequests = 0
|
||||
, shutdownCB;
|
||||
|
||||
function isAnyObject(value) {
|
||||
return value !== null && (typeof value === 'object' || typeof value === 'function');
|
||||
@ -68,12 +69,26 @@ function logglyAppender(config, layout) {
|
||||
|
||||
var msg = layout(loggingEvent);
|
||||
|
||||
openRequests++;
|
||||
|
||||
client.log({
|
||||
msg: msg,
|
||||
level: loggingEvent.level.levelStr,
|
||||
category: loggingEvent.categoryName,
|
||||
hostname: os.hostname().toString(),
|
||||
}, additionalTags);
|
||||
}, additionalTags, function (error, result) {
|
||||
if (error) {
|
||||
console.error("log4js.logglyAppender - error occurred: ", error);
|
||||
}
|
||||
|
||||
openRequests--;
|
||||
|
||||
if (shutdownCB && openRequests === 0) {
|
||||
shutdownCB();
|
||||
|
||||
shutdownCB = undefined;
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
@ -85,6 +100,15 @@ function configure(config) {
|
||||
return logglyAppender(config, layout);
|
||||
}
|
||||
|
||||
function shutdown (cb) {
|
||||
if (openRequests === 0) {
|
||||
cb();
|
||||
} else {
|
||||
shutdownCB = cb;
|
||||
}
|
||||
}
|
||||
|
||||
exports.name = 'loggly';
|
||||
exports.appender = logglyAppender;
|
||||
exports.configure = configure;
|
||||
exports.shutdown = shutdown;
|
||||
|
||||
@ -186,11 +186,16 @@ function getLogger (loggerCategoryName) {
|
||||
}
|
||||
|
||||
/**
|
||||
* args are appender, then zero or more categories
|
||||
* args are appender, optional shutdown function, then zero or more categories
|
||||
*/
|
||||
function addAppender () {
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
var appender = args.shift();
|
||||
//check for a shutdown fn
|
||||
if (args.length > 0 && typeof args[0] === 'function') {
|
||||
appenderShutdowns[appender] = args.shift();
|
||||
}
|
||||
|
||||
if (args.length === 0 || args[0] === undefined) {
|
||||
args = [ ALL_CATEGORIES ];
|
||||
}
|
||||
@ -232,6 +237,9 @@ function addAppenderToCategory(appender, category) {
|
||||
}
|
||||
|
||||
function clearAppenders () {
|
||||
//if we're calling clearAppenders, we're probably getting ready to write
|
||||
//so turn log writes back on, just in case this is after a shutdown
|
||||
loggerModule.enableAllLogWrites();
|
||||
appenders = {};
|
||||
for (var logger in loggers) {
|
||||
if (hasLogger(logger)) {
|
||||
|
||||
@ -12,10 +12,11 @@ function setupLogging(category, options) {
|
||||
createClient: function(options) {
|
||||
return {
|
||||
config: options,
|
||||
log: function(msg, tags) {
|
||||
log: function(msg, tags, cb) {
|
||||
msgs.push({
|
||||
msg: msg,
|
||||
tags: tags
|
||||
tags: tags,
|
||||
cb: cb
|
||||
});
|
||||
}
|
||||
};
|
||||
@ -49,7 +50,10 @@ function setupLogging(category, options) {
|
||||
}
|
||||
});
|
||||
|
||||
log4js.addAppender(logglyModule.configure(options), category);
|
||||
log4js.addAppender(
|
||||
logglyModule.configure(options),
|
||||
logglyModule.shutdown,
|
||||
category);
|
||||
|
||||
return {
|
||||
logger: log4js.getLogger(category),
|
||||
@ -107,4 +111,31 @@ vows.describe('log4js logglyAppender').addBatch({
|
||||
assert.deepEqual(topic.results[0].tags, []);
|
||||
}
|
||||
}
|
||||
}).addBatch({
|
||||
'with shutdown callback': {
|
||||
topic: function() {
|
||||
var setup = setupTaggedLogging();
|
||||
|
||||
setup.logger.log('trace', 'Log event #1', 'Log 2', {
|
||||
tags: ['tag1', 'tag2']
|
||||
});
|
||||
|
||||
return setup;
|
||||
},
|
||||
'after the last message has been sent': {
|
||||
topic: function (topic) {
|
||||
var that = this;
|
||||
|
||||
log4js.shutdown(this.callback);
|
||||
topic.results[0].cb();
|
||||
|
||||
// setTimeout(function() {
|
||||
// that.callback(new Error('Shutdown callback has not been called'));
|
||||
// }, 0);
|
||||
},
|
||||
'calls `log4js.shutdown`s callback function.': function(error, result) {
|
||||
assert.equal(error, undefined);
|
||||
}
|
||||
}
|
||||
}
|
||||
}).export(module);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user