Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ChrisAlderson/043337332bfd7d8ef527a6c031022fd9 to your computer and use it in GitHub Desktop.
Save ChrisAlderson/043337332bfd7d8ef527a6c031022fd9 to your computer and use it in GitHub Desktop.
/*
* file-test.js: Tests for instances of the File transport
*
* (C) 2010 Charlie Robbins
* MIT LICENSE
*
*/
const exec = require('child_process').exec;
const fs = require('fs');
const path = require('path');
const assume = require('assume');
const winston = require('../../');
const MESSAGE = Symbol.for('message');
//
// Remove all log fixtures
//
function removeFixtures(done) {
exec('rm -rf ' + path.join(__dirname, '..', 'fixtures', 'logs', 'testmaxsize*'), done);
}
describe('File (maxsize)', function () {
this.timeout(10000);
before(removeFixtures);
//after(removeFixtures);
it('should create multiple files correctly when passed more than the maxsize', function (done) {
let counter = 0;
const fillWith = ['a', 'b', 'c', 'd', 'e'];
const maxsizeTransport = new winston.transports.File({
level: 'info',
format: winston.format.printf(info => info.message),
filename: path.join(__dirname, '..', 'fixtures', 'logs', 'testmaxsize.log'),
maxsize: 4096
});
//
// Setup a list of files which we will later stat.
//
const files = [];
//
// Assets all the files have been created with the
// correct filesize
//
function assumeFilesCreated() {
files.map(function (file, i) {
let stats;
try {
stats = fs.statSync(file);
} catch (ex) {
assume(stats).is.an('object', `${file} failed to open: ${ex.message}`);
}
const text = fs.readFileSync(file, 'utf8');
// Fails to assert the 5th file because it's empty.
assume(text[0]).equals(fillWith[i]);
// Fails on each file because the eol also adds to the size.
assume(stats.size).equals(4096);
});
done();
}
//
// Log the specified kbytes to the transport
//
function logKbytes(kbytes) {
//
// Don't use shift, otherwise we cannot use the array to assert the
// results.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift
//
// const filler = fillWith.shift();
const filler = fillWith[counter];
//
//
// To not make each file not fail the assertion of the filesize we can
// make the array 1023 characters long.
//
const kbStr = Array(1024).fill(filler).join('');
fillWith.push(filler);
//
// With printf format that displays the message only
// winston adds exactly 0 characters.
//
for (var i = 0; i < kbytes; i++) {
maxsizeTransport.log({ level: 'info', [MESSAGE]: kbStr });
}
++counter;
}
maxsizeTransport.on('open', function (file) {
const match = file.match(/(\d+)\.log$/);
const count = match ? match[1] : 0;
files.push(file);
if (files.length === 5) {
return assumeFilesCreated();
}
setImmediate(() => logKbytes(4));
});
//
// Have to wait for `fs.stats` to be done in `maxsizeTransport.open()`.
// Otherwise the maxsizeTransport._dest is undefined. See https://github.com/winstonjs/winston/issues/1174
//
setTimeout(() => logKbytes(4), 1000);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment