Skip to content

Commit

Permalink
[minor] Add options.decycle option to transports.
Browse files Browse the repository at this point in the history
  • Loading branch information
coderarity committed May 6, 2012
1 parent 6cb1d48 commit 691f03f
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 15 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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]

Expand Down
12 changes: 11 additions & 1 deletion lib/winston/common.js
Expand Up @@ -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
//
Expand Down
4 changes: 3 additions & 1 deletion lib/winston/transports/console.js
Expand Up @@ -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
Expand All @@ -83,4 +84,5 @@ Console.prototype.log = function (level, msg, meta, callback) {
//
self.emit('logged');
callback(null, true);
};
};

13 changes: 11 additions & 2 deletions lib/winston/transports/couchdb.js
Expand Up @@ -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;

Expand Down
4 changes: 3 additions & 1 deletion lib/winston/transports/file.js
Expand Up @@ -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
Expand Down Expand Up @@ -329,4 +330,5 @@ File.prototype._lazyDrain = function () {
self.emit('logged');
});
}
};
};

16 changes: 14 additions & 2 deletions lib/winston/transports/loggly.js
Expand Up @@ -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;
Expand Down Expand Up @@ -158,4 +169,5 @@ Loggly.prototype.flush = function () {
//
self.logBuffer.length = 0;
});
};
};

12 changes: 7 additions & 5 deletions lib/winston/transports/transport.js
Expand Up @@ -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;
};
Expand Down Expand Up @@ -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 () { });
};
};

13 changes: 11 additions & 2 deletions lib/winston/transports/webhook.js
Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/scripts/log-exceptions.js
Expand Up @@ -22,4 +22,5 @@ logger.handleExceptions();

setTimeout(function () {
throw new Error('OH NOES! It failed!');
}, 1000);
}, 1000);

2 changes: 2 additions & 0 deletions test/helpers.js
Expand Up @@ -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;
}
};

Expand Down

0 comments on commit 691f03f

Please sign in to comment.