winston-daily-rotate-file
Advanced tools
Comparing version 1.1.5 to 1.2.0
50
index.js
@@ -69,2 +69,3 @@ 'use strict'; | ||
this.maxsize = options.maxsize || null; | ||
this.logstash = options.logstash || null; | ||
this.maxFiles = options.maxFiles || null; | ||
@@ -96,2 +97,30 @@ this.label = options.label || null; | ||
// Internal variable which will hold a record of all files | ||
// belonging to this transport which are currently in the | ||
// log directory in chronological order. | ||
// | ||
this._currentFiles = function () { | ||
// | ||
// Only proceed if maxsize is not configured for this transport. | ||
if (!this.maxsize) { | ||
try { | ||
return fs.readdirSync(this.dirname).filter(function (file) { | ||
return file.includes(this._basename); | ||
}.bind(this)).map(function (file) { | ||
return { | ||
name: file, | ||
time: fs.statSync(path.join(this.dirname, file)).mtime.getTime() | ||
}; | ||
}.bind(this)).sort(function (a, b) { | ||
return a.time - b.time; | ||
}).map(function (v) { | ||
return v.name; | ||
}); | ||
} catch (e) { | ||
// directory doesnt exist so there are no files. Do nothing. | ||
} | ||
} | ||
return []; | ||
}.bind(this)(); | ||
var now = new Date(); | ||
@@ -181,2 +210,3 @@ this._year = now.getUTCFullYear(); | ||
colorize: this.colorize, | ||
logstash: this.logstash, | ||
prettyPrint: this.prettyPrint, | ||
@@ -602,2 +632,22 @@ timestamp: this.timestamp, | ||
this._created += 1; | ||
} else if (!this.maxsize) { | ||
// | ||
// If the filename does not exist in the _currentFiles array then add it. | ||
if (this._currentFiles.indexOf(filename) === -1) { | ||
this._currentFiles.push(filename); | ||
} | ||
// While the _currentFiles array contains more file names than is configured | ||
// in maxFiles loop the _currentFiles array and delete the file found at el | ||
// 0. | ||
while (this.maxFiles && (this._currentFiles.length > this.maxFiles)) { | ||
try { | ||
fs.unlinkSync(path.join(this.dirname, this._currentFiles[0])); | ||
} catch (e) { | ||
// File isn't accessible, do nothing. | ||
} | ||
// Remove the filename that was just deleted from the _currentFiles array. | ||
this._currentFiles = this._currentFiles.slice(1); | ||
} | ||
} | ||
@@ -604,0 +654,0 @@ |
{ | ||
"name": "winston-daily-rotate-file", | ||
"version": "1.1.5", | ||
"version": "1.2.0", | ||
"description": "A transport for winston which logs to a rotating file each day.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
35
test.js
var winston = require('winston'); | ||
require('./index'); | ||
var myCustomLevels = { | ||
levels: { | ||
foo: 0, | ||
bar: 1, | ||
baz: 2, | ||
foobar: 3 | ||
}, | ||
colors: { | ||
foo: 'blue', | ||
bar: 'green', | ||
baz: 'yellow', | ||
foobar: 'red' | ||
} | ||
}; | ||
var fileTransportOpts = { | ||
filename: './server', | ||
datePattern: '.log', | ||
maxsize: 100, | ||
maxFiles: 2 | ||
filename: './colorize.log', | ||
colorize: true | ||
}; | ||
@@ -14,14 +27,6 @@ | ||
new (winston.transports.DailyRotateFile)(fileTransportOpts) | ||
] | ||
], | ||
levels: myCustomLevels.levels | ||
}); | ||
for (var i = 0; i < 100000; i++) { | ||
setTimeout(function (counter) { | ||
logger.log('info', counter.toString() + ': Hello created log files!', {}, function (err, logged) { | ||
if (err) { | ||
console.log('ERR: ', err); | ||
} | ||
console.log(counter.toString() + ': ' + logged); | ||
}); | ||
}, 250, i); | ||
} | ||
logger.foobar('Hello created log files!'); |
@@ -404,3 +404,83 @@ /* eslint-disable max-nested-callbacks,no-unused-expressions */ | ||
}); | ||
describe('when passed with maxfiles set and maxsize not set', function () { | ||
var dailyRotationPattern = { | ||
pattern: '.yyyy-MM-dd', | ||
jan1: 1861947160000, // GMT: Mon, 01 Jan 2029 07:32:40 GMT | ||
jan2: 1862033560000, // GMT: Mon, 02 Jan 2029 07:32:40 GMT | ||
jan3: 1862119960000, // GMT: Mon, 03 Jan 2029 07:32:40 GMT | ||
file1: 'test-rotation-no-maxsize.log.2029-01-01', | ||
file2: 'test-rotation-no-maxsize.log.2029-01-02', | ||
file3: 'test-rotation-no-maxsize.log.2029-01-03' | ||
}; | ||
describe('when passed the pattern ' + dailyRotationPattern.pattern + ' and no maxsize', function () { | ||
var transport; | ||
var rotationLogPath = path.join(fixturesDir, 'rotations_no_maxsize'); | ||
beforeEach(function (done) { | ||
rimraf.sync(rotationLogPath); | ||
mkdirp.sync(rotationLogPath); | ||
transport = new DailyRotateFile({ | ||
filename: path.join(rotationLogPath, 'test-rotation-no-maxsize.log'), | ||
datePattern: dailyRotationPattern.pattern, | ||
maxFiles: 2 | ||
}); | ||
done(); | ||
}); | ||
afterEach(function () { | ||
tk.reset(); | ||
}); | ||
it('should properly rotate log without maxzsize set and with old files getting deleted', function (done) { | ||
var self = this; | ||
self.time = new Date(dailyRotationPattern.jan1); | ||
tk.travel(self.time); | ||
transport.log('error', 'test message on Jan 1st', {}, function (err) { | ||
if (err) { | ||
done(err); | ||
} | ||
self.time = new Date(dailyRotationPattern.jan2); | ||
tk.travel(self.time); | ||
transport.log('error', 'test message on Jan 2nd', {}, function (err) { | ||
if (err) { | ||
done(err); | ||
} | ||
self.time = new Date(dailyRotationPattern.jan3); | ||
tk.travel(self.time); | ||
transport.log('error', 'test message on Jan 3rd', {}, function (err) { | ||
if (err) { | ||
done(err); | ||
} | ||
self.time = new Date(dailyRotationPattern.jan3); | ||
tk.travel(self.time); | ||
transport.log('error', 'second test message on Jan 3rd', {}, function (err) { | ||
if (err) { | ||
done(err); | ||
} | ||
var filesCreated = fs.readdirSync(rotationLogPath); | ||
console.log('files : ' + filesCreated); | ||
expect(filesCreated.length).to.eql(2); | ||
expect(filesCreated).not.to.include(dailyRotationPattern.file1); | ||
expect(filesCreated).to.include(dailyRotationPattern.file2); | ||
expect(filesCreated).to.include(dailyRotationPattern.file3); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
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
43588
1104