Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix undefined issue while logging from node-loggly-bulk library #11

Merged
merged 3 commits into from Jun 6, 2017
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Prev

handling both JSON and string data and stringify JSON to truncate ove…

…r 1MB
  • Loading branch information
Shweta Jain
Shweta Jain committed Jun 2, 2017
commit 1e1eac9a3e5edf6fa7375ef934dbbd086cd30015
@@ -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;
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.