diff --git a/splunklogger.js b/splunklogger.js
index 7524f7f..f611712 100644
--- a/splunklogger.js
+++ b/splunklogger.js
@@ -68,7 +68,9 @@ function _defaultEventFormatter(message, severity) {
* @property {object} config - Configuration settings for this SplunkLogger
instance.
* @param {object} requestOptions - Options to pass to {@link https://github.com/request/request#requestpost|request.post()}
.
* See the {@link http://github.com/request/request|request documentation} for all available options.
- * @property {object[]} serializedContextQueue - Queue of serialized context
objects to be sent to Splunk Enterprise or Splunk Cloud.
+ * @property {Object[]} serializedContextQueue - Queue of events and their callbacks to be sent to Splunk Enterprise or Splunk Cloud
+ * @property {Object} serializedContextQueue.event - Event definition
+ * @property {function} serializedContextQueue.callback - Callback to be invoked once event either is successfully sent, or failed to be sent
* @property {function} eventFormatter - Formats events, returning an event as a string, function(message, severity)
.
* Can be overwritten, the default event formatter will display event and severity as properties in a JSON object.
* @property {function} error - A callback function for errors: function(err, context)
.
@@ -416,16 +418,26 @@ SplunkLogger.prototype._post = function(requestOptions, callback) {
/**
* Sends events to Splunk Enterprise or Splunk Cloud, optionally with retries on non-Splunk errors.
*
- * @param context
+ * @property {Object[]} eventList - List of events to send to Splunk Enterprise or Splunk Cloud
+ * @property {Object} eventList.event - Event definition to be sent
+ * @property {function} eventList.callback - Callback to be invoked once event either is successfully sent, or failed to be sent
* @param {function} callback - A callback function: function(err, response, body)
* @private
*/
-SplunkLogger.prototype._sendEvents = function(context, callback) {
+SplunkLogger.prototype._sendEvents = function(eventList, callback) {
callback = callback || /* istanbul ignore next*/ function(){};
// Initialize the config once more to avoid undefined vals below
this.config = this._initializeConfig(this.config);
+ var data = eventList.map(function(queueItem) {
+ return queueItem.event;
+ }).join("");
+
+ var context = {
+ message: data
+ };
+
// Makes a copy of the request options so we can set the body
var requestOptions = this._initializeRequestOptions(this.requestOptions);
requestOptions.body = this._validateMessage(context.message);
@@ -495,6 +507,13 @@ SplunkLogger.prototype._sendEvents = function(context, callback) {
that.error(requestError || splunkError, context);
}
+ // call callback for each event that was sent in the request
+ for (var i = 0; i < eventList.length; i++) {
+ if (eventList[i].callback) {
+ eventList[i].callback(requestError, _response, _body);
+ }
+ }
+
callback(requestError, _response, _body);
}
);
@@ -555,7 +574,7 @@ SplunkLogger.prototype.send = function(context, callback) {
// Store the context, and its estimated length
var currentEvent = JSON.stringify(this._makeBody(context));
- this.serializedContextQueue.push(currentEvent);
+ this.serializedContextQueue.push({ event: currentEvent, callback: callback });
this.eventsBatchSize += Buffer.byteLength(currentEvent, "utf8");
var batchOverSize = this.eventsBatchSize > this.config.maxBatchSize && this.config.maxBatchSize > 0;
@@ -563,7 +582,7 @@ SplunkLogger.prototype.send = function(context, callback) {
// Only flush if the queue's byte size is too large, or has too many events
if (batchOverSize || batchOverCount) {
- this.flush(callback || function(){});
+ this.flush();
}
};
@@ -580,14 +599,8 @@ SplunkLogger.prototype.flush = function(callback) {
var queue = this.serializedContextQueue;
this.serializedContextQueue = [];
this.eventsBatchSize = 0;
-
- // Send all queued events
- var data = queue.join("");
- var context = {
- message: data
- };
- this._sendEvents(context, callback);
+ this._sendEvents(queue, callback);
};
module.exports = SplunkLogger;
diff --git a/test/test_send.js b/test/test_send.js
index f9098ee..9027787 100644
--- a/test/test_send.js
+++ b/test/test_send.js
@@ -590,9 +590,9 @@ describe("SplunkLogger send (integration tests)", function() {
// Wrap sendevents to ensure it gets called
var sendEvents = logger._sendEvents;
- logger._sendEvents = function(cont, cb) {
+ logger._sendEvents = function(queue, cb) {
sent++;
- sendEvents(cont, cb);
+ sendEvents(queue, cb);
};
logger.send(context);