From 4161ceae4b5a5311b0d68e1b5c6710da40dbfc2a Mon Sep 17 00:00:00 2001 From: Danny Arnold Date: Tue, 19 Apr 2016 22:25:01 +0200 Subject: [PATCH] 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;