bunyan-rotating-file-stream
Version:
a rotating file stream for the bunyan logging system
69 lines (51 loc) • 1.54 kB
JavaScript
// Just emits "rotate" events at the right time.
// Does not know about a rotating file stream.
;
var EventEmitter = require('events').EventEmitter;
var extend = require('lodash/extend');
var optionParser = require('./optionParser');
var nextRotTime = require('./nextrotationtime');
var setLongTimeout = require('./setlongtimeout');
var _DEBUG = false;
function PeriodRotateTrigger(options) {
var base = new EventEmitter;
var periodNum = 1;
var periodScope = 'd';
var rotAt = null;
var timeout = null;
function setupNextRotation() {
// Only step the rotation time forward if the rotation time is passed
if (rotAt <= Date.now()) {
rotAt = nextRotTime(rotAt, periodScope, periodNum);
}
timeout = setLongTimeout(rotAt, false, function () {
timeout = null;
base.emit('rotate', {date: rotAt});
});
}
function shutdown() {
if (timeout) {
timeout.clear();
}
timeout = null;
}
function newFile() {
if (timeout) {
timeout.clear();
}
setupNextRotation();
}
function logWrite() {
// Do nothing
}
var parsed = optionParser.parsePeriod(options.period);
periodScope = parsed.periodScope;
periodNum = parsed.periodNum;
setupNextRotation();
return extend({}, {
newFile: newFile,
logWrite: logWrite,
shutdown: shutdown
}, base);
};
module.exports = PeriodRotateTrigger;