log4js-node-amqp
Advanced tools
Comparing version 1.0.0 to 1.0.1
186
lib/index.js
@@ -1,9 +0,60 @@ | ||
"use strict"; | ||
var log4js = require('log4js'); | ||
var amqp = require('amqp'); | ||
'use strict' | ||
var log4js = require('log4js') | ||
var amqp = require('amqp') | ||
function amqpAppender (options) { | ||
var _options = setObjectDefaults(options, { | ||
var options | ||
var connection | ||
var sendTimer | ||
var exchange | ||
var logEventBuffer = [] | ||
function publish () { | ||
if (!exchange) { | ||
return | ||
} | ||
var toLog | ||
while (logEventBuffer.length > 0) { | ||
toLog = logEventBuffer.shift() | ||
exchange.publish( | ||
options.publish.routingKey, | ||
options.logEventInterceptor(toLog, options.additionalInfo), | ||
options.publish | ||
) | ||
} | ||
} | ||
function schedulePublish () { | ||
if (sendTimer) { | ||
return | ||
} | ||
sendTimer = setTimeout(function () { | ||
clearTimeout(sendTimer) | ||
sendTimer = null | ||
publish() | ||
}, options.sendInterval) | ||
} | ||
function setObjectDefaults (obj, defaults) { | ||
obj = obj || {} | ||
Object.keys(defaults).forEach(function (key) { | ||
if (!obj.hasOwnProperty(key)) { | ||
obj[ key ] = defaults[ key ] | ||
return | ||
} | ||
if (Object.prototype.toString.call(obj[ key ]) === '[object Object]') { | ||
return setObjectDefaults(obj[ key ], defaults[ key ]) | ||
} else { | ||
obj[ key ] = obj[ key ] | ||
} | ||
}) | ||
return obj | ||
} | ||
function amqpAppender (opts) { | ||
options = setObjectDefaults(opts, { | ||
connection: { | ||
url: "amqp://guest:guest@localhost:5672", | ||
url: 'amqp://guest:guest@localhost:5672', | ||
clientProperties: { | ||
@@ -33,99 +84,43 @@ product: 'log4js' | ||
category: logEvent.logger.category | ||
}, additionalInfo); | ||
}, additionalInfo) | ||
} | ||
}); | ||
}) | ||
_options.sendInterval *= 1000; | ||
options.sendInterval *= 1000 | ||
var sendTimer; | ||
process.once('exit', shutdown) | ||
var _exchange; | ||
var logEventBuffer = []; | ||
connection = amqp.createConnection(options.connection) | ||
process.on('exit', function () { | ||
if (connection) { | ||
connection.end(); | ||
} | ||
}); | ||
var onReady = function () { | ||
connection.once('ready', function () { | ||
// create exchange and queue (if they don't exist) and bind the queue to the exchange | ||
connection.removeListener('ready', onReady); | ||
connection.exchange(_options.exchange.name, _options.exchange, function (exchange) { | ||
_exchange = exchange; | ||
connection.exchange(options.exchange.name, options.exchange, function (ex) { | ||
exchange = ex | ||
if (!_options.queue) { | ||
return publish(); | ||
if (!options.queue) { | ||
return publish() | ||
} | ||
connection.queue(_options.queue.name, _options.queue, function (queue) { | ||
queue.bind(exchange, _options.publish.routingKey); | ||
publish(); // in case messages are waiting to be written | ||
}); | ||
}); | ||
}; | ||
connection.queue(options.queue.name, options.queue, function (queue) { | ||
queue.bind(exchange, options.publish.routingKey) | ||
publish() // in case messages are waiting to be written | ||
}) | ||
}) | ||
}) | ||
var connection = amqp.createConnection(_options.connection); | ||
connection.on('ready', onReady); | ||
function publish () { | ||
if (!_exchange) { | ||
return; | ||
} | ||
var toLog; | ||
while (logEventBuffer.length > 0) { | ||
toLog = logEventBuffer.shift(); | ||
_exchange.publish( | ||
_options.publish.routingKey, | ||
_options.logEventInterceptor(toLog, options.additionalInfo), | ||
_options.publish | ||
); | ||
} | ||
} | ||
function schedulePublish () { | ||
if (sendTimer) { | ||
return; | ||
} | ||
sendTimer = setTimeout(function () { | ||
clearTimeout(sendTimer); | ||
sendTimer = null; | ||
publish(); | ||
}, _options.sendInterval); | ||
} | ||
function setObjectDefaults (obj, defaults) { | ||
obj = obj || {}; | ||
Object.keys(defaults).forEach(function (key) { | ||
if (!obj.hasOwnProperty(key)) { | ||
obj[ key ] = defaults[ key ]; | ||
return; | ||
} | ||
if (Object.prototype.toString.call(obj[ key ]) === '[object Object]') { | ||
return setObjectDefaults(obj[ key ], defaults[ key ]); | ||
} else { | ||
obj[ key ] = obj[ key ]; | ||
} | ||
}); | ||
return obj; | ||
} | ||
return function log4jsNodeAmqp (loggingEvent) { | ||
if (Object.prototype.toString.call(loggingEvent.data[ 0 ]) === '[object String]') { | ||
loggingEvent.data = _options.layout(loggingEvent); | ||
loggingEvent.data = options.layout(loggingEvent) | ||
} else if (loggingEvent.data.length === 1) { | ||
loggingEvent.data = loggingEvent.data.shift(); | ||
loggingEvent.data = loggingEvent.data.shift() | ||
} | ||
logEventBuffer.push(loggingEvent); | ||
logEventBuffer.push(loggingEvent) | ||
if (_options.sendInterval > 0) { | ||
return schedulePublish(); | ||
if (options.sendInterval > 0) { | ||
return schedulePublish() | ||
} | ||
publish(); | ||
}; | ||
publish() | ||
} | ||
} | ||
@@ -135,10 +130,21 @@ | ||
if (config.layout) { | ||
config.layout = log4js.layouts.layout(config.layout.type, config.layout); | ||
config.layout = log4js.layouts.layout(config.layout.type, config.layout) | ||
} | ||
return amqpAppender(config); | ||
return amqpAppender(config) | ||
} | ||
exports.name = "amqp"; | ||
exports.appender = amqpAppender; | ||
exports.configure = configure; | ||
function shutdown (cb) { | ||
if (!connection) { | ||
return cb() | ||
} | ||
publish() | ||
connection.disconnect() | ||
cb() | ||
} | ||
exports.name = 'amqp' | ||
exports.appender = amqpAppender | ||
exports.configure = configure | ||
exports.shutdown = shutdown |
{ | ||
"name": "log4js-node-amqp", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "An AMQP appender for log4js-node", | ||
@@ -10,3 +10,3 @@ "main": "lib/index.js", | ||
"scripts": { | ||
"test": "node test/indexTests.js" | ||
"test": "./node_modules/.bin/standard && node test/indexTests.js" | ||
}, | ||
@@ -28,5 +28,6 @@ "repository": { | ||
"devDependencies": { | ||
"tape": "~2.3.2", | ||
"mockery": "~1.4.0" | ||
"mockery": "~1.4.0", | ||
"standard": "^5.4.1", | ||
"tape": "~2.3.2" | ||
} | ||
} |
@@ -83,4 +83,4 @@ log4js-node-amqp | ||
// see https://github.com/postwait/node-amqp#queue - specifying a queue is optional, if you don't | ||
// specify one, logs will be published to the exchange. If you do specify a queue, it will be automatically | ||
// created and bound to the exchange. | ||
// specify one, logs will be published to the exchange. If you do specify a queue, it will be | ||
// automatically created and bound to the exchange. | ||
queue: { | ||
@@ -87,0 +87,0 @@ name: 'logQ', |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
67897
125
0
3