From 7262038dd061e4a254c0e35f78231354945068c1 Mon Sep 17 00:00:00 2001 From: Shweta Jain Date: Fri, 26 May 2017 12:41:16 +0530 Subject: [PATCH 1/3] resolved TypeError --- lib/loggly/client.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/loggly/client.js b/lib/loggly/client.js index df3a527..a8e935f 100644 --- a/lib/loggly/client.js +++ b/lib/loggly/client.js @@ -105,6 +105,15 @@ util.inherits(Loggly, events.EventEmitter); // - http://www.loggly.com/docs/api-sending-data/ // Loggly.prototype.log = function (msg, tags, callback) { + + // + // typeof msg is string when we are sending logs using node-loggly-bulk. + // If we are sending logs using winston-loggly-bulk, msg is an object. + // + if (typeof(msg) === 'string') { + msg = { message: msg }; + } + msg.message = truncateLargeMessage(msg.message) if (!callback && typeof tags === 'function') { callback = tags; From 59a0019997c95fa0deb34b2a553b836e1a8e7b0d Mon Sep 17 00:00:00 2001 From: Shwetajain148 Date: Tue, 30 May 2017 18:47:36 +0530 Subject: [PATCH 2/3] handled JSON data --- lib/loggly/client.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/loggly/client.js b/lib/loggly/client.js index a8e935f..aac604b 100644 --- a/lib/loggly/client.js +++ b/lib/loggly/client.js @@ -113,8 +113,9 @@ Loggly.prototype.log = function (msg, tags, callback) { if (typeof(msg) === 'string') { msg = { message: msg }; } - - msg.message = truncateLargeMessage(msg.message) + if(msg.message) { + msg.message = truncateLargeMessage(msg.message); + } if (!callback && typeof tags === 'function') { callback = tags; tags = null; From 1e1eac9a3e5edf6fa7375ef934dbbd086cd30015 Mon Sep 17 00:00:00 2001 From: Shweta Jain Date: Fri, 2 Jun 2017 16:57:37 +0530 Subject: [PATCH 3/3] handling both JSON and string data and stringify JSON to truncate over 1MB --- lib/loggly/client.js | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/lib/loggly/client.js b/lib/loggly/client.js index aac604b..6cb99c1 100644 --- a/lib/loggly/client.js +++ b/lib/loggly/client.js @@ -28,14 +28,20 @@ function stringify(msg) { // // function to truncate message over 1 MB // -function truncateLargeMessage(message) { +function truncateLargeMessage(message) { var maximumBytesAllowedToLoggly = EVENT_SIZE; - var bytesLengthOfLogMessage = Buffer.byteLength(message); + var bytesLengthOfLogMessage = Buffer.byteLength(message); + var isMessageTruncated = false; if(bytesLengthOfLogMessage > maximumBytesAllowedToLoggly) { message = message.slice(0, maximumBytesAllowedToLoggly); + isMessageTruncated = true; } - return message; + return { + message: message, + isMessageTruncated: isMessageTruncated + }; } + // // function createClient (options) // Creates a new instance of a Loggly client. @@ -105,17 +111,18 @@ util.inherits(Loggly, events.EventEmitter); // - http://www.loggly.com/docs/api-sending-data/ // Loggly.prototype.log = function (msg, tags, callback) { - - // - // typeof msg is string when we are sending logs using node-loggly-bulk. - // If we are sending logs using winston-loggly-bulk, msg is an object. - // - if (typeof(msg) === 'string') { - msg = { message: msg }; + // typeof msg is string when we are using node-loggly-bulk to send logs. + // If we are sending logs using winston-loggly-bulk, msg is object. + // Check if 'msg' is an object, if yes then stringify it to truncate it over 1MB. + var truncatedMessageObject = null; + if(typeof(msg) === 'object'){ + var stringifiedMessage = JSON.stringify(msg) + truncatedMessageObject = truncateLargeMessage(stringifiedMessage); + msg = truncatedMessageObject.isMessageTruncated ? truncatedMessageObject.message : msg; + }else if (typeof(msg) === 'string') { + truncatedMessageObject = truncateLargeMessage(msg); + msg = truncatedMessageObject.isMessageTruncated ? truncatedMessageObject.message : msg; } - if(msg.message) { - msg.message = truncateLargeMessage(msg.message); - } if (!callback && typeof tags === 'function') { callback = tags; tags = null;