rotating-file-stream
Advanced tools
Comparing version 1.3.2 to 1.3.3
@@ -0,1 +1,4 @@ | ||
* 2017-11-13 - v1.3.3 | ||
* Solved: [problem with TypeScript](https://github.com/iccicci/rotating-file-stream/issues/19) | ||
* __immutable__ option added | ||
* 2017-09-23 - v1.3.2 | ||
@@ -2,0 +5,0 @@ * devDependencies updated |
@@ -180,2 +180,12 @@ "use strict"; | ||
function exhausted(attempts) { | ||
var err = new Error("Too many destination file attempts"); | ||
err.code = "RFS-TOO-MANY"; | ||
if(attempts) | ||
err.attempts = attempts; | ||
return err; | ||
} | ||
function findName(attempts, tmp, callback) { | ||
@@ -187,11 +197,5 @@ var count = 0; | ||
if(count >= 1000) { | ||
var err = new Error("Too many destination file attempts"); | ||
if(count >= 1000) | ||
return callback(this.exhausted(attempts)); | ||
err.attempts = attempts; | ||
err.code = "RFS-TOO-MANY"; | ||
return callback(err); | ||
} | ||
var name = this.name + "." + count + ".rfs.tmp"; | ||
@@ -243,3 +247,3 @@ var self = this; | ||
out.once("finish", process.nextTick.bind(process, callback)); | ||
out.once("finish", callback); | ||
@@ -271,2 +275,3 @@ inp.pipe(zip).pipe(out); | ||
compress: compress, | ||
exhausted: exhausted, | ||
external: external, | ||
@@ -273,0 +278,0 @@ findName: findName, |
78
index.js
@@ -36,5 +36,2 @@ "use strict"; | ||
if(options.rotationTime && options.initialRotation) | ||
options.initialRotation = null; | ||
var opt = {}; | ||
@@ -53,2 +50,3 @@ | ||
this.size = 0; | ||
this.write = this.write; // https://github.com/iccicci/rotating-file-stream/issues/19 | ||
@@ -158,12 +156,6 @@ utils.setEvents(this); | ||
try { | ||
this.name = this.generator(null); | ||
} | ||
catch(err) { | ||
return process.nextTick(function() { | ||
self.emit("error", err); | ||
process.nextTick(self._rewrite.bind(self)); | ||
}); | ||
} | ||
if(this.options.immutable) | ||
return this.immutate(true); | ||
this.name = this.generator(null); | ||
this.once("open", this.interval.bind(this)); | ||
@@ -182,3 +174,3 @@ | ||
if(self.options.interval && self.options.initialRotation) { | ||
if(self.options.initialRotation) { | ||
var prev; | ||
@@ -206,2 +198,51 @@ | ||
RotatingFileStream.prototype.immutate = function(first, index, now) { | ||
if(! index) { | ||
index = 1; | ||
now = this.now(); | ||
} | ||
if(index >= 1001) | ||
return this.emit("error", this.exhausted()); | ||
if(index === 1) | ||
this.last = this.name; | ||
try{ this.name = this.generator(now, index); } | ||
catch(e) { return this.emit("error", e); } | ||
var open = function() { | ||
this.open(); | ||
this.once("open", function() { | ||
if(! first) { | ||
this.emit("rotation", this.name); | ||
this.emit("rotated", this.last); | ||
} | ||
this.interval(); | ||
}.bind(this)); | ||
}.bind(this); | ||
fs.stat(this.name, function(err, stats) { | ||
this.size = 0; | ||
if(err) { | ||
if(err.code === "ENOENT") | ||
return open(); | ||
return this.emit("error", err); | ||
} | ||
if(! stats.isFile()) | ||
return this.emit("error", new Error("Can't write on: " + this.name + " (it is not a file)")); | ||
this.size = stats.size; | ||
if(this.options.size && stats.size >= this.options.size) | ||
return this.immutate(first, index + 1, now); | ||
open(); | ||
}.bind(this)); | ||
} | ||
RotatingFileStream.prototype.move = function(retry) { | ||
@@ -289,2 +330,3 @@ var name; | ||
RotatingFileStream.prototype.rotate = function() { | ||
var mutable = this.options.rotate || ! this.options.immutable; | ||
this.size = 0; | ||
@@ -294,4 +336,10 @@ this.rotation = new Date(); | ||
this._clear(); | ||
this._close(this.options.rotate ? this.classical.bind(this, this.options.rotate) : this.move.bind(this)); | ||
this.emit("rotation"); | ||
this._close(mutable ? | ||
this.options.rotate ? | ||
this.classical.bind(this, this.options.rotate) : | ||
this.move.bind(this) : | ||
this.immutate.bind(this)); | ||
if(mutable) | ||
this.emit("rotation"); | ||
}; | ||
@@ -298,0 +346,0 @@ |
@@ -13,3 +13,3 @@ "use strict"; | ||
function _interval(now) { | ||
function __interval(now) { | ||
now = new Date(now); | ||
@@ -36,12 +36,7 @@ var year = now.getFullYear(); | ||
function interval() { | ||
if(! this.options.interval) | ||
return; | ||
var now = this.now(); | ||
function _interval(now) { | ||
var unit = this.options.interval.unit; | ||
if(unit === "d" || unit === "h") { | ||
this._interval(now); | ||
} | ||
if(unit === "d" || unit === "h") | ||
this.__interval(now); | ||
else { | ||
@@ -57,2 +52,12 @@ var period = 1000 * this.options.interval.num; | ||
return new Date(this.prev); | ||
} | ||
function interval() { | ||
if(! this.options.interval) | ||
return; | ||
var now = this.now(); | ||
this._interval(now); | ||
this.timer = setTimeout(this.rotate.bind(this), this.next - now); | ||
@@ -71,2 +76,4 @@ this.timer.unref(); | ||
self.emit("warning", err); | ||
self.emit("history"); | ||
}); | ||
@@ -158,6 +165,7 @@ } | ||
module.exports = { | ||
_clear: _clear, | ||
_interval: _interval, | ||
history: history, | ||
interval: interval, | ||
__interval: __interval, | ||
_clear: _clear, | ||
_interval: _interval, | ||
history: history, | ||
interval: interval, | ||
}; |
{ | ||
"name": "rotating-file-stream", | ||
"version": "1.3.2", | ||
"version": "1.3.3", | ||
"description": "Opens a stream.Writable to a file rotated by interval and/or size. A logrotate alternative.", | ||
"scripts": { | ||
"all": "npm install && npm run npmignore && npm run eslint && npm run coverage", | ||
"all": "npm run npmignore && npm run eslint && npm run coverage", | ||
"coverage": "TZ=\"Europe/Rome\" ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- --recursive test", | ||
"eslint": "./node_modules/.bin/eslint *.js test/*js", | ||
"npmignore": "echo '.codeclimate.yml\\n.eslintrc\\n.gitignore\\n.travis.yml\\nCHANGELOG.md\\npackage-lock.json\\ntest' > .npmignore ; cat .gitignore >> .npmignore", | ||
"test": "TZ=\"Europe/Rome\" ./node_modules/.bin/_mocha test" | ||
"test": "bash -c 'if [[ `node --version` =~ ^v0.1 ]] ; then npm install mocha@3.5.3 ; fi' ; TZ=\"Europe/Rome\" ./node_modules/.bin/_mocha test" | ||
}, | ||
@@ -30,7 +30,7 @@ "bugs": "https://github.com/iccicci/rotating-file-stream/issues", | ||
"devDependencies": { | ||
"eslint": "4.7.2", | ||
"eslint": "4.10.0", | ||
"istanbul": "0.4.5", | ||
"mocha-istanbul": "0.3.0", | ||
"mocha": "3.5.3" | ||
"mocha": "4.0.1", | ||
"mocha-istanbul": "0.3.0" | ||
} | ||
} |
# rotating-file-stream | ||
[![Build Status](https://travis-ci.org/iccicci/rotating-file-stream.png)](https://travis-ci.org/iccicci/rotating-file-stream) | ||
[![Build Status](https://travis-ci.org/iccicci/rotating-file-stream.png?branch=master)](https://travis-ci.org/iccicci/rotating-file-stream?branch=master) | ||
[![Code Climate](https://codeclimate.com/github/iccicci/rotating-file-stream/badges/gpa.svg)](https://codeclimate.com/github/iccicci/rotating-file-stream) | ||
@@ -84,3 +84,3 @@ [![Test Coverage](https://codeclimate.com/github/iccicci/rotating-file-stream/badges/coverage.svg)](https://codeclimate.com/github/iccicci/rotating-file-stream/coverage) | ||
return "/storage/" + month + "/" + month + | ||
return month + "/" + month + | ||
day + "-" + hour + minute + "-" + index + "-file.log"; | ||
@@ -121,3 +121,4 @@ } | ||
* history: {String} (default: null) Specifies the _history filename_. | ||
* initialRotation: {Boolean} (default: null) Eventually makes an initial rotation based on _not-rotated file_ timestamp. | ||
* immutable: {Boolean} (default: null) Never mutates file names. | ||
* initialRotation: {Boolean} (default: null) Initial rotation based on _not-rotated file_ timestamp. | ||
* interval: {String} (default: null) Specifies the time interval to rotate the file. | ||
@@ -129,3 +130,3 @@ * maxFiles: {Integer} (default: null) Specifies the maximum number of rotated files to keep. | ||
* rotate: {Integer} (default: null) Enables the classical UNIX __logrotate__ behaviour. | ||
* rotationTime: {Boolean} (default: null) Makes rotated file name with time of rotation instead of start time of period. | ||
* rotationTime: {Boolean} (default: null) Makes rotated file name with time of rotation. | ||
* size: {String} (default: null) Specifies the file size to rotate the file. | ||
@@ -223,11 +224,8 @@ | ||
__Note:__ | ||
this option is ignored if __immutable__ is set to __true__. | ||
__Note:__ | ||
the shell command to compress the rotated file should not remove the source file, it will be removed by the package | ||
if rotation job complete with success. | ||
#### rotationTime | ||
As specified above, if rotation by interval is enabled, the parameter _time_ passed to _rotatle name generator_ is the | ||
start time of rotation period. Setting this option to __true__, parameter _time_ passed is time when rotation job | ||
started. | ||
#### initialRotation | ||
@@ -250,2 +248,24 @@ When program stops in a rotation period then restarts in a new rotation period, logs of differente rotation period will | ||
#### immutable | ||
If set to __true__, names of generated files never changes. In other words the _rotated file name generator_ is never | ||
called with a __null__ _time_ parameter and new files are immediately generated with their rotated name. | ||
__rotation__ _envet_ now has a _filename_ paramere with the newly created file name. | ||
Usefull to send logs to logstash through filebeat. | ||
__Note:__ | ||
if this option is set to __true__, __compress__ is ignored. | ||
__Note:__ | ||
this option is ignored if __interval__ is not set. | ||
#### rotationTime | ||
As specified above, if rotation by interval is enabled, the parameter _time_ passed to _rotatle name generator_ is the | ||
start time of rotation period. Setting this option to __true__, parameter _time_ passed is time when rotation job | ||
started. | ||
__Note:__ | ||
if this option is set to __true__, __initialRotation__ is ignored. | ||
#### history | ||
@@ -290,4 +310,5 @@ | ||
stream.on('rotation', function() { | ||
stream.on('rotation', function(filename) { | ||
// rotation job started | ||
// filename: if immutable then newly generated file else null | ||
}); | ||
@@ -294,0 +315,0 @@ |
22
utils.js
@@ -123,2 +123,4 @@ "use strict"; | ||
"immutable": function() {}, | ||
"initialRotation": function() {}, | ||
@@ -166,2 +168,22 @@ | ||
if(! ret.interval) { | ||
delete ret.immutable; | ||
delete ret.initialRotation; | ||
delete ret.rotationTime; | ||
} | ||
if(ret.rotate) { | ||
delete ret.history; | ||
delete ret.immutable; | ||
delete ret.maxFiles; | ||
delete ret.maxSize; | ||
delete ret.rotationTime; | ||
} | ||
if(ret.immutable) | ||
delete ret.compress; | ||
if(ret.rotationTime) | ||
delete ret.initialRotation; | ||
return ret; | ||
@@ -168,0 +190,0 @@ } |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
40489
785
384
1