hmpo-logger
Advanced tools
Comparing version 7.0.0 to 7.0.1
@@ -115,5 +115,5 @@ const debug = require('debug')('hmpo:logger:transport:file'); | ||
_removeOldFiles() { | ||
_removeOldFiles(cb) { | ||
const maxFiles = this._options.maxFiles; | ||
if (!maxFiles) return; | ||
if (!maxFiles) return cb && cb(null, []); | ||
@@ -123,5 +123,4 @@ const pattern = path.join(this._dirname, this._basename + '-*' + this._extname); | ||
debug('Finding old log files with pattern', pattern); | ||
this._dateRotateGlob(pattern, (err, files) => { | ||
if (err) return debug('Error removing old log files', err); | ||
if (!files || files.length <= maxFiles) return; | ||
this._dateRotateGlob.glob(pattern).then(files => { | ||
if (!files || files.length <= maxFiles) return cb && cb(null, []); | ||
@@ -134,2 +133,6 @@ const oldFiles = files.sort().reverse().slice(maxFiles); | ||
}); | ||
return cb && cb(null, oldFiles); | ||
}).catch(err => { | ||
debug('Error removing old log files', err); | ||
return cb && cb(err); | ||
}); | ||
@@ -136,0 +139,0 @@ } |
{ | ||
"name": "hmpo-logger", | ||
"version": "7.0.0", | ||
"version": "7.0.1", | ||
"description": "Consistent logging for hmpo apps", | ||
@@ -29,13 +29,13 @@ "main": "index.js", | ||
"deep-clone-merge": "^1.5.5", | ||
"glob": "^10.2.2", | ||
"glob": "^10.2.6", | ||
"on-finished": "^2.4.1", | ||
"on-headers": "^1.0.1", | ||
"sort-object-keys": "^1.1.2" | ||
"on-headers": "^1.0.2", | ||
"sort-object-keys": "^1.1.3" | ||
}, | ||
"devDependencies": { | ||
"chai": "^4.3.7", | ||
"eslint": "^8.39.0", | ||
"eslint": "^8.41.0", | ||
"mocha": "^10.2.0", | ||
"nyc": "^15.1.0", | ||
"sinon": "^15.0.4", | ||
"sinon": "^15.1.0", | ||
"sinon-chai": "^3.7.0", | ||
@@ -42,0 +42,0 @@ "sinon-test": "^3.1.5" |
@@ -5,2 +5,3 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const glob = require('glob'); | ||
@@ -299,3 +300,3 @@ | ||
describe('_removeOldFiles', function () { | ||
let transport, cb; | ||
let transport, globPromise; | ||
@@ -305,3 +306,4 @@ beforeEach(function () { | ||
sinon.stub(fs, 'unlink').yields(); | ||
transport._dateRotateGlob = sinon.stub(); | ||
globPromise = sinon.promise(); | ||
transport._dateRotateGlob = { glob: sinon.stub().returns(globPromise) }; | ||
}); | ||
@@ -313,7 +315,10 @@ | ||
it('does not remove files if maxFiles is zero', function () { | ||
it('does not remove files if maxFiles is zero', function (done) { | ||
transport._options.maxFiles = 0; | ||
transport._removeOldFiles(); | ||
transport._dateRotateGlob.should.not.have.been.called; | ||
fs.unlink.should.not.have.been.called; | ||
transport._removeOldFiles(() => { | ||
fs.unlink.should.not.have.been.called; | ||
done(); | ||
}); | ||
transport._dateRotateGlob.glob.should.not.have.been.called; | ||
}); | ||
@@ -323,10 +328,16 @@ | ||
transport._removeOldFiles(); | ||
transport._dateRotateGlob.should.have.been.calledWithExactly( | ||
'/path/test-*.log', | ||
sinon.match.func | ||
transport._dateRotateGlob.glob.should.have.been.calledWithExactly( | ||
'/path/test-*.log' | ||
); | ||
}); | ||
it('calls fs.unlink for each of the oldest files outside of maxFiles', function () { | ||
transport._dateRotateGlob.yields(null, [ | ||
it('calls fs.unlink for each of the oldest files outside of maxFiles', function (done) { | ||
transport._removeOldFiles(() => { | ||
fs.unlink.should.have.been.calledWithExactly('/path/test-2016-07-02.log', sinon.match.func); | ||
fs.unlink.should.have.been.calledWithExactly('/path/test-2016-04-30.log', sinon.match.func); | ||
fs.unlink.should.have.been.calledWithExactly('/path/test-2015-11-14.log', sinon.match.func); | ||
done(); | ||
}); | ||
globPromise.resolve([ | ||
'/path/test-2016-11-15.log', | ||
@@ -339,10 +350,11 @@ '/path/test-2016-07-02.log', | ||
]); | ||
transport._removeOldFiles(); | ||
fs.unlink.should.have.been.calledWithExactly('/path/test-2016-07-02.log', sinon.match.func); | ||
fs.unlink.should.have.been.calledWithExactly('/path/test-2016-04-30.log', sinon.match.func); | ||
fs.unlink.should.have.been.calledWithExactly('/path/test-2015-11-14.log', sinon.match.func); | ||
}); | ||
it('does not remove files if number of files is equal to maxFiles', function () { | ||
transport._dateRotateGlob.yields(null, [ | ||
it('does not remove files if number of files is equal to maxFiles', function (done) { | ||
transport._removeOldFiles(() => { | ||
fs.unlink.should.not.have.been.called; | ||
done(); | ||
}); | ||
globPromise.resolve([ | ||
'/path/test-2015-11-14.log', | ||
@@ -352,26 +364,64 @@ '/path/test-2016-07-02.log', | ||
]); | ||
transport._removeOldFiles(cb); | ||
fs.unlink.should.not.have.been.called; | ||
}); | ||
it('does not remove files if number of files is less than maxFiles', function () { | ||
transport._dateRotateGlob.yields(null, [ | ||
it('does not remove files if number of files is less than maxFiles', function (done) { | ||
transport._removeOldFiles(() => { | ||
fs.unlink.should.not.have.been.called; | ||
done(); | ||
}); | ||
globPromise.resolve([ | ||
'/path/test-2015-11-14.log', | ||
'/path/test-2016-04-30.log' | ||
]); | ||
transport._removeOldFiles(cb); | ||
fs.unlink.should.not.have.been.called; | ||
}); | ||
it('does not remove files if there is a glob error', function () { | ||
transport._dateRotateGlob.yields(null); | ||
transport._removeOldFiles(); | ||
fs.unlink.should.not.have.been.called; | ||
it('does not remove files if glob returns nothing', function (done) { | ||
transport._removeOldFiles(() => { | ||
fs.unlink.should.not.have.been.called; | ||
done(); | ||
}); | ||
transport._dateRotateGlob.yields({ message: 'Error' }); | ||
transport._removeOldFiles(); | ||
fs.unlink.should.not.have.been.called; | ||
globPromise.resolve(); | ||
}); | ||
it('does not remove files if there is a glob error', function (done) { | ||
transport._removeOldFiles(() => { | ||
fs.unlink.should.not.have.been.called; | ||
done(); | ||
}); | ||
globPromise.reject(new Error()); | ||
}); | ||
}); | ||
describe('_removeOldFiles glob', function () { | ||
let transport; | ||
beforeEach(function () { | ||
sinon.stub(fs, 'unlink').yields(); | ||
}); | ||
afterEach(function () { | ||
fs.unlink.restore(); | ||
}); | ||
it('does not call fs.unlink if there are no files to remove', function (done) { | ||
transport = new FileTransport({ name, formatter, filename: path.resolve(__dirname, '..', 'fixtures', 'otherlog.logfile'), dateRotate: true, maxFiles: 2 }); | ||
transport._removeOldFiles(() => { | ||
fs.unlink.should.not.have.been.called; | ||
done(); | ||
}); | ||
}); | ||
it('calls fs.unlink for each of the oldest files outside of maxFiles', function (done) { | ||
transport = new FileTransport({ name, formatter, filename: path.resolve(__dirname, '..', 'fixtures', 'test.logfile'), dateRotate: true, maxFiles: 2 }); | ||
transport._removeOldFiles(() => { | ||
fs.unlink.should.have.been.calledWithExactly(sinon.match('test-2016-11-15.logfile'), sinon.match.func); | ||
fs.unlink.should.have.been.calledWithExactly(sinon.match('test-2016-12-13.logfile'), sinon.match.func); | ||
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
124667
43
2803
Updatedglob@^10.2.6
Updatedon-headers@^1.0.2
Updatedsort-object-keys@^1.1.3