From 4161ceae4b5a5311b0d68e1b5c6710da40dbfc2a Mon Sep 17 00:00:00 2001 From: Danny Arnold Date: Tue, 19 Apr 2016 22:25:01 +0200 Subject: [PATCH 1/2] add a shutdown handler to loggly --- lib/appenders/loggly.js | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/appenders/loggly.js b/lib/appenders/loggly.js index c13682b..851ee85 100644 --- a/lib/appenders/loggly.js +++ b/lib/appenders/loggly.js @@ -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'); @@ -50,7 +51,7 @@ function processTags(msgListArgs) { * { * token: 'your-really-long-input-token', * subdomain: 'your-subdomain', - * tags: ['loggly-tag1', 'loggly-tag2', .., 'loggly-tagn'] + * tags: ['loggly-tag1', 'loggly-tag2', .., 'loggly-tagn'] * } * @param layout a function that takes a logevent and returns a string (defaults to objectLayout). */ @@ -68,13 +69,27 @@ 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 - Logging to loggly, error happende ", error); + } + + openRequests--; + + if (shutdownCB && openRequests === 0) { + shutdownCB(); + + shutdownCB = undefined; + } + }); + }; } function configure(config) { @@ -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; From b7bc12374113789c22d03a799f882764e3efca24 Mon Sep 17 00:00:00 2001 From: Danny Arnold Date: Tue, 19 Apr 2016 22:25:20 +0200 Subject: [PATCH 2/2] add not functional tests for the shutdown handler --- test/logglyAppender-test.js | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/test/logglyAppender-test.js b/test/logglyAppender-test.js index 688e43e..adf1202 100644 --- a/test/logglyAppender-test.js +++ b/test/logglyAppender-test.js @@ -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 }); } }; @@ -107,4 +108,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);