Skip to content

Commit

Permalink
Adding fieldOrder feature to put fields in the required order
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Tupper committed Feb 26, 2016
1 parent 619ca6d commit 53de21f
Showing 1 changed file with 43 additions and 6 deletions.
49 changes: 43 additions & 6 deletions lib/rotatingfilestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,52 @@ function RotatingFileStream(options) {
var stream = null;
var streambytesWritten = 0;

function nullJsonify(textlog) {
return textlog;
}

function fastJsonify(rawlog) {
return JSON.stringify(rawlog, bunyan.safeCycles()) + '\n';
}

function orderedJsonify(rawlog) {
var log = {};

for (var fieldsortindex = 0; options.fieldOrder && fieldsortindex < options.fieldOrder.length; fieldsortindex += 1) {
if (rawlog.hasOwnProperty(options.fieldOrder[fieldsortindex])) {
log[options.fieldOrder[fieldsortindex]] = rawlog[options.fieldOrder[fieldsortindex]];
}
}

for (var k in rawlog) {
log[k] = rawlog[k];
}

return JSON.stringify(log, bunyan.safeCycles()) + '\n';
}

function chooseJsonify(log) {
if (typeof (log) === 'string' && options.fieldOrder) {
base.emit('error', 'Can only set fieldOrder with the stream set to "raw"');
}

if (typeof (log) === 'string') {
jsonify = nullJsonify;
} else if (options.fieldOrder) {
jsonify = orderedJsonify;
} else {
jsonify = fastJsonify;
}

return jsonify(log);
};

var jsonify = chooseJsonify;

var forceWrite = false;
function writer(logs, callback) {
for (var i = 0; stream && i < logs.length; i += 1) {
var str;
if (typeof (logs[i]) === 'string') {
str = logs[i];
} else {
str = JSON.stringify(logs[i], bunyan.safeCycles()) + '\n';
}
var str = jsonify(logs[i]);

var writeBuffer = new Buffer(str, 'utf8');

Expand Down

0 comments on commit 53de21f

Please sign in to comment.