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

Add metaKey config option. Fix JSON encoding of meta Error instance. #38

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ The Loggly transport is based on [Nodejitsu's][2] [node-loggly][3] implementatio
* __tags:__ An array of tags to send to loggly.
* __isBulk:__ If true, sends messages using bulk url
* __stripColors:__ Strip color codes from the logs before sending
* __metaKey:__ If defined, keys from the meta parameter are written to the loggly message under this key rather than at the top level


*Metadata:* Logged in suggested [Loggly format][5]
Expand Down
42 changes: 33 additions & 9 deletions lib/winston-loggly.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ var Loggly = exports.Loggly = function (options) {

this.timestamp = options.timestamp || false;
this.stripColors = options.stripColors || false;
this.metaKey = options.metaKey;
};

//
Expand Down Expand Up @@ -89,20 +90,43 @@ Loggly.prototype.log = function (level, msg, meta, callback) {
return callback(null, true);
}

if (this.timestamp && (!meta || !meta.timestamp)) {
meta = meta || {};
meta.timestamp = (new Date()).toISOString();
var entry = {}, self = this;

if (meta && meta.timestamp) {
entry.timestamp = meta.timestamp;
} else if (this.timestamp) {
entry.timestamp = (new Date()).toISOString();
}

if (this.stripColors) {
msg = ('' + msg).replace(code, '');
}

var message = winston.clone(meta || {}),
self = this;
function copyProperties(src, dest) {
Object.getOwnPropertyNames(src).forEach(function(k) {
dest[k] = src[k];
});
}

if (Array.isArray(meta)) {
if (this.metaKey !== undefined) {
// winston.clone() won't work for array. ideally ought to deep clone here as winston.clone() would do
entry[this.metaKey] = meta.slice();
} else {
//write the array values into the entry directly as numbered items (winston.clone() does this)
copyProperties(winston.clone(meta), entry);
}
} else if (Object.keys(meta).length !== 0 || meta instanceof Error) {
if (this.metaKey !== undefined) {
entry[this.metaKey] = {};
copyProperties(winston.clone(meta), entry[this.metaKey]);
} else {
copyProperties(winston.clone(meta), entry);
}
}

message.level = level;
message.message = msg;
entry.level = level;
entry.message = msg;

//
// Helper function for responded to logging.
Expand All @@ -113,8 +137,8 @@ Loggly.prototype.log = function (level, msg, meta, callback) {
}

return meta.tags
? this.client.log(message, meta.tags, logged)
: this.client.log(message, logged);
? this.client.log(entry, meta.tags, logged)
: this.client.log(entry, logged);
};

//
Expand Down