From 691f03f3051db67233ef05423e83ad5eac032283 Mon Sep 17 00:00:00 2001 From: Christian Howe Date: Sun, 6 May 2012 17:45:50 +0000 Subject: [PATCH] [minor] Add `options.decycle` option to transports. --- README.md | 3 +++ lib/winston/common.js | 12 +++++++++++- lib/winston/transports/console.js | 4 +++- lib/winston/transports/couchdb.js | 13 +++++++++++-- lib/winston/transports/file.js | 4 +++- lib/winston/transports/loggly.js | 16 ++++++++++++++-- lib/winston/transports/transport.js | 12 +++++++----- lib/winston/transports/webhook.js | 13 +++++++++++-- test/fixtures/scripts/log-exceptions.js | 3 ++- test/helpers.js | 2 ++ 10 files changed, 67 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 01e1203a6..1f8ae5f5b 100644 --- a/README.md +++ b/README.md @@ -456,6 +456,7 @@ The Console transport takes two simple options: * __silent:__ Boolean flag indicating whether to suppress output (default false). * __colorize:__ Boolean flag indicating if we should colorize output (default false). * __timestamp:__ Boolean flag indicating if we should prepend output with timestamps (default false). If function is specified, its return value will be used instead of timestamps. +* __decycle:__ Boolean flag indicating if `meta` should be [decycled](https://github.com/douglascrockford/JSON-js). *Metadata:* Logged via util.inspect(meta); @@ -475,6 +476,7 @@ The File transport should really be the 'Stream' transport since it will accept * __maxFiles:__ Limit the number of files created when the size of the logfile is exceeded. * __stream:__ The WriteableStream to write output to. * __json:__ If true, messages will be logged as JSON (default true). +* __decycle:__ Boolean flag indicating if `meta` should be [decycled](https://github.com/douglascrockford/JSON-js). *Metadata:* Logged via util.inspect(meta); @@ -491,6 +493,7 @@ The Loggly transport is based on [Nodejitsu's][5] [node-loggly][6] implementatio * __inputName:__ The name of the input this instance should log to. * __inputToken:__ The input token of the input this instance should log to. * __json:__ If true, messages will be sent to Loggly as JSON. +* __decycle:__ Boolean flag indicating if `meta` should be [decycled](https://github.com/douglascrockford/JSON-js). *Metadata:* Logged in suggested [Loggly format][2] diff --git a/lib/winston/common.js b/lib/winston/common.js index 01da77ba3..b6aca58f1 100644 --- a/lib/winston/common.js +++ b/lib/winston/common.js @@ -109,9 +109,19 @@ exports.clone = function (obj) { exports.log = function (options) { var timestampFn = typeof options.timestamp === 'function' ? options.timestamp : exports.timestamp, timestamp = options.timestamp ? timestampFn() : null, - meta = options.meta ? exports.clone(cycle.decycle(options.meta)) : null, + meta, output; + if (options.decycle && options.meta) { + meta = cycle.decycle(options.meta); + } + else if (options.meta) { + meta = exports.clone(options.meta); + } + else { + meta = null; + } + // // raw mode is intended for outputing winston as streaming JSON to STDOUT // diff --git a/lib/winston/transports/console.js b/lib/winston/transports/console.js index 8e3db6241..9271a2f94 100644 --- a/lib/winston/transports/console.js +++ b/lib/winston/transports/console.js @@ -66,6 +66,7 @@ Console.prototype.log = function (level, msg, meta, callback) { level: level, message: msg, meta: meta, + decycle: this.decycle, stringify: this.stringify, timestamp: this.timestamp, raw: this.raw @@ -83,4 +84,5 @@ Console.prototype.log = function (level, msg, meta, callback) { // self.emit('logged'); callback(null, true); -}; \ No newline at end of file +}; + diff --git a/lib/winston/transports/couchdb.js b/lib/winston/transports/couchdb.js index 335528172..a400f1c01 100644 --- a/lib/winston/transports/couchdb.js +++ b/lib/winston/transports/couchdb.js @@ -67,11 +67,20 @@ Couchdb.prototype.log = function (level, msg, meta, callback) { } var self = this, - meta = cycle.decycle(meta), - message = common.clone(meta || {}), options, + message, req; + if (this.decycle && meta) { + message = meta = cycle.decycle(meta); + } + else if (meta) { + message = common.clone(meta); + } + else { + message = {}; + } + message.level = level; message.message = msg; diff --git a/lib/winston/transports/file.js b/lib/winston/transports/file.js index 14ed39d2c..de59c2d32 100644 --- a/lib/winston/transports/file.js +++ b/lib/winston/transports/file.js @@ -93,6 +93,7 @@ File.prototype.log = function (level, msg, meta, callback) { level: level, message: msg, meta: meta, + decycle: this.decycle, json: this.json, colorize: this.colorize, timestamp: this.timestamp @@ -329,4 +330,5 @@ File.prototype._lazyDrain = function () { self.emit('logged'); }); } -}; \ No newline at end of file +}; + diff --git a/lib/winston/transports/loggly.js b/lib/winston/transports/loggly.js index f46dffba5..f721a6cee 100644 --- a/lib/winston/transports/loggly.js +++ b/lib/winston/transports/loggly.js @@ -95,7 +95,18 @@ Loggly.prototype.log = function (level, msg, meta, callback) { } var self = this, - message = common.clone(meta || {}); + message; + + if (options.decycle && meta) { + message = meta = cycle.decycle(options.meta); + } + else if (meta) { + message = common.clone(options.meta); + } + else { + meta = {}; + } + message.level = level; message.message = msg; @@ -158,4 +169,5 @@ Loggly.prototype.flush = function () { // self.logBuffer.length = 0; }); -}; \ No newline at end of file +}; + diff --git a/lib/winston/transports/transport.js b/lib/winston/transports/transport.js index 76e7ce949..0c15daeab 100644 --- a/lib/winston/transports/transport.js +++ b/lib/winston/transports/transport.js @@ -18,10 +18,11 @@ var events = require('events'), var Transport = exports.Transport = function (options) { events.EventEmitter.call(this); - options = options || {}; - this.level = options.level || 'info'; - this.silent = options.silent || false; - this.raw = options.raw || false; + options = options || {}; + this.level = options.level || 'info'; + this.silent = options.silent || false; + this.raw = options.raw || false; + this.decycle = options.decycle || false; this.handleExceptions = options.handleExceptions || false; }; @@ -56,4 +57,5 @@ Transport.prototype.logException = function (msg, meta, callback) { this.once('logged', onLogged); this.once('error', onError); this.log('error', msg, meta, function () { }); -}; \ No newline at end of file +}; + diff --git a/lib/winston/transports/webhook.js b/lib/winston/transports/webhook.js index ac1a676e7..2db63d4c2 100644 --- a/lib/winston/transports/webhook.js +++ b/lib/winston/transports/webhook.js @@ -68,11 +68,20 @@ Webhook.prototype.log = function (level, msg, meta, callback) { } var self = this, - meta = cycle.decycle(meta), - message = common.clone(meta), options, + message, req; + if (this.decycle && meta) { + message = meta = cycle.decycle(meta); + } + else if (meta) { + message = common.clone(meta); + } + else { + message = {}; + } + message.level = level; message.message = msg; diff --git a/test/fixtures/scripts/log-exceptions.js b/test/fixtures/scripts/log-exceptions.js index 43ce7ebcf..91e8a4192 100644 --- a/test/fixtures/scripts/log-exceptions.js +++ b/test/fixtures/scripts/log-exceptions.js @@ -22,4 +22,5 @@ logger.handleExceptions(); setTimeout(function () { throw new Error('OH NOES! It failed!'); -}, 1000); \ No newline at end of file +}, 1000); + diff --git a/test/helpers.js b/test/helpers.js index b160123c8..845ef8136 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -180,7 +180,9 @@ helpers.testLevels = function (levels, transport, assertMsg, assertFn) { var circmetadatatest = { topic: function () { + transport.decycle = true; transport.log('info', 'test message', circmetadata, this.callback.bind(this, null)); + transport.decycle = false; } };