node-loggly-bulk
Advanced tools
Comparing version 2.0.3 to 2.1.0
@@ -161,3 +161,3 @@ /* | ||
'content-type': this.json ? 'application/json' : 'text/plain', | ||
'content-length': Buffer.byteLength(msg) | ||
'content-length': Buffer.byteLength(JSON.stringify(msg)) | ||
} | ||
@@ -189,7 +189,11 @@ }; | ||
try { | ||
var result = JSON.parse(body); | ||
self.emit('log', result); | ||
if (callback) { | ||
callback(null, result); | ||
if(body && res.statusCode.toString() === '200'){ | ||
var result = JSON.parse(body); | ||
self.emit('log', result); | ||
if (callback) { | ||
callback(null, result); | ||
} | ||
} | ||
else | ||
console.log('Error Code- ' + res.statusCode + ' "' + res.statusMessage + '"'); | ||
} | ||
@@ -196,0 +200,0 @@ catch (ex) { |
@@ -17,3 +17,3 @@ /* | ||
// | ||
// Variables for buffer array | ||
// Variables for buffer array | ||
// | ||
@@ -28,2 +28,11 @@ var arrBufferedMsg = [], | ||
// | ||
// Variables for server retry | ||
// | ||
var arrRetryLogs = [], | ||
maxRetryAllowed = 5, | ||
totalRetries = 0, | ||
statusCode, | ||
notFailedOnServerError = true; | ||
var https = require('https'), | ||
@@ -49,3 +58,4 @@ util = require('util'), | ||
501: 'Not Implemented', | ||
503: 'Throttled' | ||
503: 'Throttled', | ||
504: 'Gateway Timeout' | ||
}; | ||
@@ -163,14 +173,21 @@ | ||
requestOptions.body = requestBody; | ||
arrRetryLogs = arrRetryLogs.concat(requestBody); | ||
} | ||
function sendLogs() { | ||
if (arrRetryLogs.length && !requestBody) requestOptions.body = arrRetryLogs[0]; | ||
try { | ||
request(requestOptions, function (err, res, body) { | ||
if (err) { | ||
return onError(err); | ||
if (err) return onError(err); | ||
statusCode = res.statusCode.toString(); | ||
if(statusCode === '403') isValidToken = false; | ||
if (statusCode === '500' || statusCode === '503' || statusCode === '504') retryOnServerError(res); | ||
if (statusCode === '200') { | ||
arrRetryLogs.splice(0, 1); | ||
totalRetries = 0; | ||
} | ||
var statusCode = res.statusCode.toString(); | ||
if(statusCode === '403') isValidToken = false; | ||
if (Object.keys(failCodes).indexOf(statusCode) !== -1) { | ||
return onError((new Error('Loggly Error (' + statusCode + '): ' + failCodes[statusCode]))); | ||
} | ||
if (statusCode !== '503' && statusCode !== '500' && statusCode !== '504') { | ||
return onError((new Error('Loggly Error (' + statusCode + '): ' + failCodes[statusCode]))); | ||
} | ||
} | ||
success(res, body); | ||
@@ -184,4 +201,7 @@ }); | ||
function sendBulkLogs() { | ||
if (arrMsg.length === 0) { | ||
return; | ||
if (arrMsg.length === 0 && arrRetryLogs.length === 0) return; | ||
var retryLogs = []; | ||
if (arrRetryLogs.length && !arrMsg.length) { | ||
retryLogs = arrRetryLogs.slice(0, arrSize); | ||
requestOptions.body = retryLogs.join('\n'); | ||
} | ||
@@ -191,13 +211,20 @@ // | ||
// | ||
requestOptions.body = arrMsg.join('\n'); | ||
arrMsg.length = 0; | ||
if (arrMsg.length) { | ||
requestOptions.body = arrMsg.join('\n'); | ||
arrMsg.length = 0; | ||
} | ||
try { | ||
request(requestOptions, function (err, res, body) { | ||
if (err) { | ||
return onError(err); | ||
} | ||
if (err) return onError(err); | ||
var statusCode = res.statusCode.toString(); | ||
if(statusCode === '403') isValidToken = false; | ||
if (statusCode === '500' || statusCode === '503' || statusCode === '504') retryOnServerError(res); | ||
if (statusCode === '200') { | ||
arrRetryLogs.splice(0, arrSize); | ||
totalRetries = 0; | ||
} | ||
if (Object.keys(failCodes).indexOf(statusCode) !== -1) { | ||
return onError((new Error('Loggly Error (' + statusCode + '): ' + failCodes[statusCode]))); | ||
if (statusCode !== '503' && statusCode !== '500' && statusCode !== '504') { | ||
return onError((new Error('Loggly Error (' + statusCode + '): ' + failCodes[statusCode]))); | ||
} | ||
} | ||
@@ -237,2 +264,23 @@ success(res, body); | ||
// | ||
//function to retry sending logs maximum 5 times if server error occurs | ||
// | ||
function retryOnServerError(err) { | ||
if (!arrRetryLogs.length) return; | ||
else { | ||
if (notFailedOnServerError && totalRetries >= maxRetryAllowed) { | ||
console.log('Failed after ' + totalRetries + ' retries on error - ' + statusCode, '"'+err.statusMessage+'"'); | ||
notFailedOnServerError = false; | ||
arrRetryLogs.length = 0; | ||
totalRetries = 0; | ||
} | ||
while (isValidToken && totalRetries < maxRetryAllowed) { | ||
console.log('Failed on error code ' + statusCode); | ||
console.log('Retried ' + (totalRetries + 1) + ' time'); | ||
totalRetries++; | ||
isBulk ? sendBulkLogs() : sendLogs(); | ||
} | ||
} | ||
} | ||
// | ||
// retries to send buffered logs to loggly in every 30 seconds | ||
@@ -261,3 +309,3 @@ // | ||
if(err) return; | ||
var statusCode = res.statusCode.toString(); | ||
statusCode = res.statusCode.toString(); | ||
if(statusCode === "200") { | ||
@@ -264,0 +312,0 @@ arrBufferedMsg.splice(0, logsInBunch); |
{ | ||
"name": "node-loggly-bulk", | ||
"description": "A client implementation for Loggly cloud Logging-as-a-Service API", | ||
"version": "2.0.3", | ||
"version": "2.1.0", | ||
"author": "Charlie Robbins <charlie.robbins@gmail.com>", | ||
@@ -17,5 +17,5 @@ "repository": { | ||
"dependencies": { | ||
"request": ">=2.76.0 <3.0.0", | ||
"moment": "^2.18.1", | ||
"json-stringify-safe": "5.0.x" | ||
"json-stringify-safe": "^5.0.1", | ||
"moment": "^2.19.1", | ||
"request": "^2.83.0" | ||
}, | ||
@@ -22,0 +22,0 @@ "devDependencies": { |
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
59291
1601
1
Updatedjson-stringify-safe@^5.0.1
Updatedmoment@^2.19.1
Updatedrequest@^2.83.0