bunyan-rotating-file-stream
Advanced tools
Comparing version 1.2.0 to 1.2.1
# bunyan Changelog | ||
## 1.2.1 | ||
- Catching write errors and emitting them | ||
- Ensuring a stream is closed before finishing a join (internal testing method) | ||
- Handling events in the underlying stream | ||
## 1.2.0 | ||
@@ -4,0 +10,0 @@ |
@@ -48,8 +48,2 @@ 'use strict'; | ||
function shutdownCurrentStream(next) { | ||
base.emit('closefile'); | ||
stream.end(next); | ||
stream = null; | ||
}; | ||
function gzipCurrentFile(next) { | ||
@@ -164,2 +158,6 @@ if (!gzip) { | ||
function streamErrorHandler(err) { | ||
base.emit('error', err); | ||
}; | ||
function initialiseNewFile(next) { | ||
@@ -174,2 +172,4 @@ var filePath = getNumberedFile(0, false) | ||
stream.on('error', streamErrorHandler); | ||
stream.once('open', function () { | ||
@@ -182,5 +182,18 @@ base.emit('newfile', { stream: stream, path: streamPath }); | ||
}); | ||
}; | ||
function shutdownCurrentStream(next) { | ||
base.emit('closefile'); | ||
var streamCopy = stream; | ||
stream.end(function () { | ||
streamCopy.removeListener('error', streamErrorHandler); | ||
if (next) { | ||
next(); | ||
} | ||
}); | ||
stream = null; | ||
}; | ||
// Calling rotate gives us a new stream | ||
@@ -212,5 +225,5 @@ // Once called, the previous stream is not valid and | ||
return _.extend({}, { initialise, rotate }, base); | ||
return _.extend({}, { initialise, rotate, end: shutdownCurrentStream }, base); | ||
} | ||
module.exports = FileRotator; |
@@ -12,2 +12,4 @@ // Just emits "rotate" events at the right time. | ||
var setLongTimeout = require ('./setlongtimeout'); | ||
var _DEBUG = false; | ||
@@ -25,33 +27,18 @@ | ||
function setupNextRot() { | ||
if (!rotAt || rotAt <= Date.now()) { | ||
function setupNextRotation() { | ||
// Only step the rotation time forward if the rotation time is passed | ||
if (rotAt <= Date.now()) { | ||
rotAt = nextRotTime(rotAt, periodScope, periodNum); | ||
} | ||
var delay = rotAt - Date.now(); | ||
// Cap timeout to Node's max setTimeout, see | ||
// <https://github.com/joyent/node/issues/8656>. | ||
var TIMEOUT_MAX = 2147483647; // 2^31-1 | ||
if (delay > TIMEOUT_MAX) { | ||
delay = TIMEOUT_MAX; | ||
} | ||
timeout = setTimeout(function () { | ||
// If rotation period is > ~25 days, we have to break into multiple | ||
// setTimeout's. See <https://github.com/joyent/node/issues/8656>. | ||
if (rotAt && rotAt > Date.now()) { | ||
return setupNextRot(); | ||
} else { | ||
base.emit('rotate'); | ||
} | ||
}, delay); | ||
if (typeof (timeout.unref) === 'function') { | ||
timeout.unref(); | ||
} | ||
timeout = setLongTimeout(rotAt, false, function () { | ||
timeout = null; | ||
base.emit('rotate'); | ||
}); | ||
} | ||
function shutdown() { | ||
clearTimeout(timeout); | ||
if (timeout) { | ||
timeout.clear(); | ||
} | ||
timeout = null; | ||
@@ -61,4 +48,6 @@ } | ||
function reset() { | ||
clearTimeout(timeout); | ||
setupNextRot(); | ||
if (timeout) { | ||
timeout.clear(); | ||
} | ||
setupNextRotation(); | ||
} | ||
@@ -74,3 +63,3 @@ | ||
setupNextRot(); | ||
setupNextRotation(); | ||
@@ -77,0 +66,0 @@ return _.extend({}, {reset, check, shutdown}, base); |
@@ -49,10 +49,14 @@ // A rotating file stream will just | ||
stream.write(writeBuffer, function (err) { | ||
try { | ||
stream.write(writeBuffer, function (err) { | ||
if (err) { | ||
base.emit('error', err); | ||
} | ||
}); | ||
}); | ||
} catch (err) { | ||
base.emit('error', err); | ||
} | ||
streambytesWritten += writeBuffer.byteLength; | ||
base.emit('data', { bytesWritten: stream.bytesWritten }); | ||
base.emit('data', { bytesWritten: streambytesWritten }); | ||
} else { | ||
@@ -147,3 +151,6 @@ // We suddenly don't have a stream, save this log for later | ||
function join(cb) { | ||
writeQueue.join(cb); | ||
writeQueue.join(function () { | ||
rotator.end(cb); | ||
}); | ||
} | ||
@@ -150,0 +157,0 @@ |
{ | ||
"name": "bunyan-rotating-file-stream", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "a rotating file stream for the bunyan logging system", | ||
@@ -26,5 +26,5 @@ "author": "Jim Tupper <npm@tupper.org.uk> (http://github.com/rcomian)", | ||
"devDependencies": { | ||
"mkdir-recursive": "^0.2.1", | ||
"rmdir": "^1.1.0", | ||
"why-is-node-running": "^1.2.1" | ||
"mkdir-recursive": "^0.2.1", | ||
"rmdir": "^1.1.0", | ||
"why-is-node-running": "^1.2.1" | ||
}, | ||
@@ -31,0 +31,0 @@ "dependencies": { |
@@ -23,3 +23,3 @@ Bunyan is **a simple and fast JSON logging library** for node.js services: | ||
## 1.2.0 | ||
## 1.2 | ||
@@ -32,2 +32,3 @@ - Support non-raw streams. For some reason, raw streams are MUCH faster in high load scenarios (at least when this is the only stream). | ||
- Using setImmediate rather than process.nextTick in the write loop to allow io and other operations time to happen rather than hog the event loop. | ||
- Other refactorings and minor bug fixes. | ||
@@ -34,0 +35,0 @@ # Current Status |
85
test.js
@@ -10,2 +10,4 @@ var bunyan = require('bunyan'); | ||
var InitialPeriodRotateTrigger = require('./lib/initialperiodtrigger'); | ||
var setLongTimeout = require('./lib/setlongtimeout'); | ||
var whyRunning; | ||
@@ -238,2 +240,80 @@ | ||
function checksetlongtimeout(next) { | ||
var name = 'testlogs/' + 'checksetlongtimeout'; | ||
var d = new Date(); | ||
d.setSeconds(d.getSeconds() + 10); | ||
var calls = 0; | ||
var longtimeout = setLongTimeout(d.getTime(), true, function () { | ||
clearTimeout(catchall); | ||
calls += 1; | ||
if (calls > 1) { | ||
console.log('Called multiple times when expecting one!', calls); | ||
} else { | ||
console.log(name, 'passed'); | ||
next(); | ||
} | ||
}, 100); | ||
var catchall = setTimeout(function () { | ||
console.log(name, 'FAILED: Timer did not fire'); | ||
next('FAILED: Timer did not fire'); | ||
if (longtimeout) { | ||
longtimeout.clear(); | ||
} | ||
}, 15000); | ||
} | ||
function checksetlongtimeoutclear(next) { | ||
var name = 'testlogs/' + 'checksetlongtimeoutclear'; | ||
var d = new Date(); | ||
d.setSeconds(d.getSeconds() + 10); | ||
var calls = 0; | ||
var longtimeout = setLongTimeout(d.getTime(), true, function () { | ||
calls += 1; | ||
}, 100); | ||
setTimeout(function () { | ||
longtimeout.clear(); | ||
}, 5000); | ||
setTimeout(function () { | ||
if (calls === 0) { | ||
console.log(name, 'passed'); | ||
next(); | ||
} else { | ||
console.log(name, 'FAILED: Timer fired even when it was cleared'); | ||
} | ||
}, 11000); | ||
} | ||
function checksetlongtimeoutclearnormalperiods(next) { | ||
var name = 'testlogs/' + 'checksetlongtimeoutclearnormalperiods'; | ||
var d = new Date(); | ||
d.setSeconds(d.getSeconds() + 10); | ||
var calls = 0; | ||
var longtimeout = setLongTimeout(d.getTime(), true, function () { | ||
calls += 1; | ||
}); | ||
setTimeout(function () { | ||
longtimeout.clear(); | ||
}, 5000); | ||
setTimeout(function () { | ||
if (calls === 0) { | ||
next(); | ||
console.log(name, 'passed'); | ||
} else { | ||
console.log(name, 'FAILED: Timer fired even when it was cleared'); | ||
} | ||
}, 11000); | ||
} | ||
async.parallel([ | ||
@@ -247,3 +327,6 @@ basicthreshold, | ||
checkrotationofoldfile, | ||
checkrotationofnewfile | ||
checkrotationofnewfile, | ||
checksetlongtimeout, | ||
checksetlongtimeoutclear, | ||
checksetlongtimeoutclearnormalperiods | ||
], function (err) { | ||
@@ -250,0 +333,0 @@ if (err) console.log(err); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
57484913
35
1181
207