From 55fdc480ea764b5fd0bd7dfc9e048b0be90de404 Mon Sep 17 00:00:00 2001 From: Jim Tupper Date: Fri, 26 Feb 2016 23:12:20 +0000 Subject: [PATCH] Refactoring and adding in support for fast json serialisation without cycle checking --- lib/rotatingfilestream.js | 82 +++++++++++++++++++++++---------------- package.json | 1 - 2 files changed, 49 insertions(+), 34 deletions(-) diff --git a/lib/rotatingfilestream.js b/lib/rotatingfilestream.js index e347b07..1898b81 100644 --- a/lib/rotatingfilestream.js +++ b/lib/rotatingfilestream.js @@ -31,12 +31,31 @@ function RotatingFileStream(options) { var stream = null; var streambytesWritten = 0; + // Copied from bunyan source + function safeCycles() { + var seen = []; + return function (key, val) { + if (!val || typeof (val) !== 'object') { + return val; + } + if (seen.indexOf(val) !== -1) { + return '[Circular]'; + } + seen.push(val); + return val; + }; + } + function nullJsonify(textlog) { return textlog; } function fastJsonify(rawlog) { - return JSON.stringify(rawlog, bunyan.safeCycles()) + '\n'; + return JSON.stringify(rawlog, safeCycles()) + '\n'; + } + + function fastUnsafeJsonify(rawlog) { + return JSON.stringify(rawlog) + '\n'; } function orderedJsonify(rawlog) { @@ -52,7 +71,7 @@ function RotatingFileStream(options) { log[k] = rawlog[k]; } - return JSON.stringify(log, bunyan.safeCycles()) + '\n'; + return JSON.stringify(log, safeCycles()) + '\n'; } function chooseJsonify(log) { @@ -64,6 +83,8 @@ function RotatingFileStream(options) { jsonify = nullJsonify; } else if (options.fieldOrder) { jsonify = orderedJsonify; + } else if (options.noCyclesCheck) { + jsonify = fastUnsafeJsonify; } else { jsonify = fastJsonify; } @@ -73,8 +94,8 @@ function RotatingFileStream(options) { var jsonify = chooseJsonify; - var forceWrite = false; function writer(logs, callback) { + var written = -1; // the index of the last successful write for (var i = 0; stream && i < logs.length; i += 1) { var str = jsonify(logs[i]); @@ -82,35 +103,26 @@ function RotatingFileStream(options) { streambytesWritten += writeBuffer.byteLength; - if (forceWrite) { - // This log entry gets written regardless - don't emit - forceWrite = false; - } else { - base.emit('data', { bytesWritten: streambytesWritten }); - } - - if (!stream) { - // This log entry caused a rotation - // We can't write it, force it to be written - // Next time round - forceWrite = true; - break; - } + base.emit('data', { bytesWritten: streambytesWritten }); - try { - stream.write(writeBuffer, function (err) { - if (err) { + if (stream) { + try { + stream.write(writeBuffer, function (err) { + if (err) { + base.emit('error', err); + } + }); + } catch (err) { base.emit('error', err); } - }); - } catch (err) { - base.emit('error', err); + + written = i; } } // If we didn't get all the way through the array, unshift the remaining // records back onto our queue in reverse order - for (var rollback = logs.length -1; rollback >= i; rollback -= 1) { + for (var rollback = logs.length -1; rollback > written; rollback -= 1) { writeQueue.unshift(logs[rollback]); } @@ -155,23 +167,27 @@ function RotatingFileStream(options) { }); } - var rotating = false; - function rotate() { - if (rotating) { - return; - } else { - rotating = true; - } + function rotateActual() { + + base.once('data', function () { + rotateFunction = rotateActual; + }); + + rotateFunction = function () {}; rotator.rotate(function (err, newstream, filePath) { if (err) { base.emit('error', err); } - - rotating = false; }); } + var rotateFunction = rotateActual; + + function rotate() { + rotateFunction(); + } + function write(s, callback) { writeQueue.push(s, callback); } diff --git a/package.json b/package.json index 76e1ef0..1ee1422 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,6 @@ "logging", "log4j", "json", - "bunyan", "rotating", "rotate" ],