From 2750d423cc97a2bd26237ff1271fd6c78f33df70 Mon Sep 17 00:00:00 2001 From: Tom Haigh Date: Thu, 3 Dec 2015 16:19:54 +0000 Subject: [PATCH 1/3] Add metaKey config option. Fix JSON encoding of meta Error instance. --- README.md | 1 + lib/winston-loggly.js | 32 +++++++++++++++++++++++++------- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index dc6143a..9c75988 100644 --- a/README.md +++ b/README.md @@ -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] diff --git a/lib/winston-loggly.js b/lib/winston-loggly.js index bd6c3a2..af88afc 100644 --- a/lib/winston-loggly.js +++ b/lib/winston-loggly.js @@ -58,6 +58,7 @@ var Loggly = exports.Loggly = function (options) { this.timestamp = options.timestamp || false; this.stripColors = options.stripColors || false; + this.metaKey = options.metaKey; }; // @@ -89,20 +90,37 @@ Loggly.prototype.log = function (level, msg, meta, callback) { return callback(null, true); } + var entry = {}; + if (this.timestamp && (!meta || !meta.timestamp)) { - meta = meta || {}; - meta.timestamp = (new Date()).toISOString(); + entry.timestamp = (new Date()).toISOString(); + } else { + entry.timestamp = meta.timestamp + delete meta.timestamp } if (this.stripColors) { msg = ('' + msg).replace(code, ''); } - var message = winston.clone(meta || {}), + var meta = meta ? winston.clone(meta) : undefined, self = this; - message.level = level; - message.message = msg; + function copyProperties(src, dest) { + Object.getOwnPropertyNames(src).forEach(function(k) { + dest[k] = src[k]; + }); + } + + if (this.metaKey !== undefined) { + entry[this.metaKey] = {}; + copyProperties(meta, entry[this.metaKey]); + } else { + copyProperties(meta, entry); + } + + entry.level = level; + entry.message = msg; // // Helper function for responded to logging. @@ -113,8 +131,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); }; // From 2d6a9d3084cd06d7b25e2d3472e1241bf3c23d1a Mon Sep 17 00:00:00 2001 From: Tom Haigh Date: Thu, 18 Feb 2016 09:13:56 +0000 Subject: [PATCH 2/3] Better handling of arrays. Hide empty meta objects --- lib/winston-loggly.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/winston-loggly.js b/lib/winston-loggly.js index af88afc..bb8e941 100644 --- a/lib/winston-loggly.js +++ b/lib/winston-loggly.js @@ -103,20 +103,27 @@ Loggly.prototype.log = function (level, msg, meta, callback) { msg = ('' + msg).replace(code, ''); } - var meta = meta ? winston.clone(meta) : undefined, - self = this; - function copyProperties(src, dest) { Object.getOwnPropertyNames(src).forEach(function(k) { dest[k] = src[k]; }); } - if (this.metaKey !== undefined) { - entry[this.metaKey] = {}; - copyProperties(meta, entry[this.metaKey]); - } else { - copyProperties(meta, entry); + 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); + } } entry.level = level; From b166a6e4e2d9098ad7b57a07018aa181c0d691ba Mon Sep 17 00:00:00 2001 From: Tom Haigh Date: Thu, 18 Feb 2016 09:14:58 +0000 Subject: [PATCH 3/3] Copy (not move) timestamp if in meta and simplify logic --- lib/winston-loggly.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/winston-loggly.js b/lib/winston-loggly.js index bb8e941..9570998 100644 --- a/lib/winston-loggly.js +++ b/lib/winston-loggly.js @@ -90,13 +90,12 @@ Loggly.prototype.log = function (level, msg, meta, callback) { return callback(null, true); } - var entry = {}; + var entry = {}, self = this; - if (this.timestamp && (!meta || !meta.timestamp)) { + if (meta && meta.timestamp) { + entry.timestamp = meta.timestamp; + } else if (this.timestamp) { entry.timestamp = (new Date()).toISOString(); - } else { - entry.timestamp = meta.timestamp - delete meta.timestamp } if (this.stripColors) {