bunyan-rotating-file-stream
Advanced tools
Comparing version 1.0.6 to 1.1.0
@@ -5,2 +5,6 @@ # bunyan Changelog | ||
## 1.1.0 | ||
- Adding ability to rotate existing log files when we open them up with the "rotateExisting" feature. | ||
## 1.0.6 | ||
@@ -7,0 +11,0 @@ |
@@ -6,2 +6,3 @@ // Utility to orchestrate the rotating file system object and its triggers. | ||
var PeriodTrigger = require('./lib/periodtrigger'); | ||
var InitialPeriodTrigger = require('./lib/initialperiodtrigger'); | ||
var ThresholdTrigger = require('./lib/thresholdtrigger'); | ||
@@ -18,2 +19,7 @@ var TriggerAdapter = require('./lib/triggeradapter'); | ||
if (options.period && options.rotateExisting) { | ||
var initialPeriodTrigger = InitialPeriodTrigger(options); | ||
TriggerAdapter(initialPeriodTrigger, rfs); | ||
} | ||
if (options.threshold) { | ||
@@ -24,2 +30,4 @@ var thresholdTrigger = ThresholdTrigger(options); | ||
rfs.initialise(); | ||
return rfs; | ||
@@ -26,0 +34,0 @@ } |
@@ -171,7 +171,10 @@ 'use strict'; | ||
base.emit('newfile', { stream: stream, path: streamPath }); | ||
stream.once('open', function () { | ||
base.emit('newfile', { stream: stream, path: streamPath }); | ||
if (next) { | ||
next(); | ||
} | ||
if (next) { | ||
next(); | ||
} | ||
}); | ||
}; | ||
@@ -178,0 +181,0 @@ |
@@ -10,85 +10,6 @@ // Just emits "rotate" events at the right time. | ||
var optionParser = require('./optionParser'); | ||
var nextRotTime = require('./nextrotationtime'); | ||
var _DEBUG = false; | ||
function nextRotTime(rotAt, periodScope, periodNum) { | ||
if (_DEBUG) | ||
console.log('-- _nextRotTime: %s%s', periodNum, periodScope); | ||
var d = new Date(); | ||
if (_DEBUG) console.log(' now local: %s', d); | ||
if (_DEBUG) console.log(' now utc: %s', d.toISOString()); | ||
var newRotAt; | ||
switch (periodScope) { | ||
case 'ms': | ||
// Hidden millisecond period for debugging. | ||
if (rotAt) { | ||
newRotAt = rotAt + periodNum; | ||
} else { | ||
newRotAt = Date.now() + periodNum; | ||
} | ||
break; | ||
case 'h': | ||
if (rotAt) { | ||
newRotAt = rotAt + periodNum * 60 * 60 * 1000; | ||
} else { | ||
// First time: top of the next hour. | ||
newRotAt = Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), | ||
d.getUTCDate(), d.getUTCHours() + 1); | ||
} | ||
break; | ||
case 'd': | ||
if (rotAt) { | ||
newRotAt = rotAt + periodNum * 24 * 60 * 60 * 1000; | ||
} else { | ||
// First time: start of tomorrow (i.e. at the coming midnight) UTC. | ||
newRotAt = Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), | ||
d.getUTCDate() + 1); | ||
} | ||
break; | ||
case 'w': | ||
// Currently, always on Sunday morning at 00:00:00 (UTC). | ||
if (rotAt) { | ||
newRotAt = rotAt + periodNum * 7 * 24 * 60 * 60 * 1000; | ||
} else { | ||
// First time: this coming Sunday. | ||
newRotAt = Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), | ||
d.getUTCDate() + (7 - d.getUTCDay())); | ||
} | ||
break; | ||
case 'm': | ||
if (rotAt) { | ||
newRotAt = Date.UTC(d.getUTCFullYear(), | ||
d.getUTCMonth() + periodNum, 1); | ||
} else { | ||
// First time: the start of the next month. | ||
newRotAt = Date.UTC(d.getUTCFullYear(), d.getUTCMonth() + 1, 1); | ||
} | ||
break; | ||
case 'y': | ||
if (rotAt) { | ||
newRotAt = Date.UTC(d.getUTCFullYear() + periodNum, 0, 1); | ||
} else { | ||
// First time: the start of the next year. | ||
newRotAt = Date.UTC(d.getUTCFullYear() + 1, 0, 1); | ||
} | ||
break; | ||
default: | ||
assert.fail('invalid period scope: "' + periodScope + '"'); | ||
} | ||
if (_DEBUG) { | ||
console.log(' **rotAt**: %s (utc: %s)', newRotAt, | ||
new Date(rotAt).toUTCString()); | ||
var now = Date.now(); | ||
console.log(' now: %s (%sms == %smin == %sh to go)', | ||
now, | ||
newRotAt - now, | ||
(newRotAt-now)/1000/60, | ||
(newRotAt-now)/1000/60/60); | ||
} | ||
return newRotAt; | ||
}; | ||
function PeriodRotateTrigger(options) { | ||
@@ -95,0 +16,0 @@ var base = new EventEmitter; |
@@ -61,10 +61,2 @@ // A rotating file stream will just | ||
rotator.initialise(function (err, newstream, filePath) { | ||
if (err) { | ||
base.emit('error', err); | ||
} | ||
writeQueue.resume(); | ||
}); | ||
rotator.on('error', function (err) { | ||
@@ -79,2 +71,12 @@ base.emit('error', err); | ||
function initialise() { | ||
rotator.initialise(function (err, newstream, filePath) { | ||
if (err) { | ||
base.emit('error', err); | ||
} | ||
writeQueue.resume(); | ||
}); | ||
} | ||
function rotate() { | ||
@@ -118,2 +120,3 @@ if (writeQueue.paused()) { | ||
stream, | ||
initialise, | ||
rotate, | ||
@@ -120,0 +123,0 @@ write, |
{ | ||
"name": "bunyan-rotating-file-stream", | ||
"version": "1.0.6", | ||
"version": "1.1.0", | ||
"description": "a rotating file stream for the bunyan logging system", | ||
@@ -27,3 +27,4 @@ "author": "Jim Tupper <npm@tupper.org.uk> (http://github.com/rcomian)", | ||
"mkdir-recursive": "^0.2.1", | ||
"rmdir": "^1.1.0" | ||
"rmdir": "^1.1.0", | ||
"why-is-node-running": "^1.2.1" | ||
}, | ||
@@ -30,0 +31,0 @@ "dependencies": { |
@@ -110,2 +110,11 @@ Bunyan is **a simple and fast JSON logging library** for node.js services: | ||
<tr> | ||
<td>rotateExisting</td> | ||
<td>No</td> | ||
<td>false</td> | ||
<td>If period is also set, will rotate an existing log file when the process | ||
starts up if that file would have been rotated due to its age. This means that | ||
if you want a new file every day, and the process isn't running overnight, | ||
when you start up the next day you'll get a new file anyway.</td> | ||
</tr> | ||
<tr> | ||
<td>threshold</td> | ||
@@ -112,0 +121,0 @@ <td>No</td> |
62
test.js
@@ -9,2 +9,4 @@ var bunyan = require('bunyan'); | ||
var async = require('async'); | ||
var InitialPeriodRotateTrigger = require('./lib/initialperiodtrigger'); | ||
var whyRunning = require('why-is-node-running') | ||
@@ -86,3 +88,3 @@ function runTest(name, options, next) { | ||
}, | ||
function (next) { rmdir(name, next); } | ||
function (next) { rmdir(name, ignoreMissing(next)); } | ||
], next); | ||
@@ -107,3 +109,3 @@ } | ||
}, | ||
function (next) { rmdir(name, next); } | ||
function (next) { rmdir(name, ignoreMissing(next)); } | ||
], next); | ||
@@ -129,3 +131,3 @@ } | ||
}, | ||
function (next) { rmdir(name, next); } | ||
function (next) { rmdir(name, ignoreMissing(next)); } | ||
], next); | ||
@@ -150,3 +152,3 @@ } | ||
}, | ||
function (next) { rmdir(name, next); } | ||
function (next) { rmdir(name, ignoreMissing(next)); } | ||
], next); | ||
@@ -171,3 +173,3 @@ } | ||
}, | ||
function (next) { rmdir(name, next); } | ||
function (next) { rmdir(name, ignoreMissing(next)); } | ||
], next); | ||
@@ -192,7 +194,44 @@ } | ||
}, | ||
function (next) { rmdir(name, next); } | ||
function (next) { rmdir(name, ignoreMissing(next)); } | ||
], next); | ||
} | ||
function checkrotationofoldfile(next) { | ||
var name = 'testlogs/' + 'checkrotationofoldfile'; | ||
var periodtrigger = InitialPeriodRotateTrigger({ period: '1h' }); | ||
var rotations = 0; | ||
periodtrigger.on('rotate', function () { | ||
rotations += 1; | ||
}); | ||
periodtrigger.checkIfRotationNeeded(Date.now() - (10000 * 60 * 60 * 3)); | ||
setTimeout(function () { | ||
assert.equal(1, rotations); | ||
console.log(name, 'passed'); | ||
next(); | ||
}, 1000); | ||
} | ||
function checkrotationofnewfile(next) { | ||
var name = 'testlogs/' + 'checkrotationofnewfile'; | ||
var periodtrigger = InitialPeriodRotateTrigger({ period: '1h' }); | ||
var rotations = 0; | ||
periodtrigger.on('rotate', function () { | ||
rotations += 1; | ||
}); | ||
periodtrigger.checkIfRotationNeeded(Date.now() - (3)); | ||
setTimeout(function () { | ||
assert.equal(0, rotations); | ||
console.log(name, 'passed'); | ||
next(); | ||
}, 1000); | ||
} | ||
async.parallel([ | ||
@@ -204,5 +243,14 @@ basicthreshold, | ||
totalfiles, | ||
shorthandperiod | ||
shorthandperiod, | ||
checkrotationofoldfile, | ||
checkrotationofnewfile | ||
], function (err) { | ||
if (err) console.log(err); | ||
}); | ||
var totalTimeout = setTimeout(function () { | ||
console.log('Still running: '); | ||
whyRunning(); | ||
}, 20000); | ||
totalTimeout.unref(); |
50735
22
909
183
3
5