Skip to content

Commit

Permalink
Log file without .gz for tailable (winstonjs#1525) (winstonjs#1526)
Browse files Browse the repository at this point in the history
* Log file without .gz for tailable (winstonjs#1525)

* Convert vows based file-archive-test
* Do not append .gz to log file if tailable and zippedArchive are true

* Fix file-archive.test
  • Loading branch information
pixtron authored and DABH committed Nov 29, 2018
1 parent f87be3e commit ed27e85
Showing 3 changed files with 94 additions and 84 deletions.
2 changes: 1 addition & 1 deletion lib/winston/transports/file.js
Original file line number Diff line number Diff line change
@@ -592,7 +592,7 @@ module.exports = class File extends TransportStream {
? `${basename}${isRotation}${ext}`
: `${basename}${ext}`;

return this.zippedArchive
return this.zippedArchive && !this.tailable
? `${target}.gz`
: target;
}
83 changes: 0 additions & 83 deletions test/transports/file-archive-test.js

This file was deleted.

93 changes: 93 additions & 0 deletions test/transports/file-archive.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* file-archive-test.js: Tests for instances of the File transport setting the archive option,
*
* (C) 2015 Nimrod Becker
* MIT LICENSE
*
*/

/* eslint-disable no-sync */
const assert = require('assert');
const rimraf = require('rimraf');
const fs = require('fs');
const path = require('path');
const winston = require('../../lib/winston');

const { MESSAGE } = require('triple-beam');

//
// Remove all log fixtures
//
function removeFixtures(done) {
rimraf(path.join(__dirname, '..', 'fixtures', 'logs', 'testarchive*'), done);
}


let archiveTransport = null;

describe('winston/transports/file/zippedArchive', function () {
describe('An instance of the File Transport with tailable true', function () {
before(removeFixtures);
after(removeFixtures);

it('init logger AFTER cleaning up old files', function () {
archiveTransport = new winston.transports.File({
timestamp: true,
json: false,
zippedArchive: true,
tailable: true,
filename: 'testarchive.log',
dirname: path.join(__dirname, '..', 'fixtures', 'logs'),
maxsize: 4096,
maxFiles: 3
});
});

it('when created archived files are rolled', function (done) {
let created = 0;
let loggedTotal = 0;

function data(ch, kb) {
return String.fromCharCode(65 + ch).repeat(kb * 1024 - 1);
}

function logKbytes(kbytes, txt) {
const toLog = {};
toLog[MESSAGE] = data(txt, kbytes);
archiveTransport.log(toLog);
}

archiveTransport.on('logged', function (info) {
loggedTotal += info[MESSAGE].length + 1;
if (loggedTotal >= 14 * 1024) { // just over 3 x 4kb files
return done();
}

if (loggedTotal % 4096 === 0) {
created++;
}
// eslint-disable-next-line max-nested-callbacks
setTimeout(() => logKbytes(1, created), 1);
});

logKbytes(1, created);
});

it('should be only 3 files called testarchive.log, testarchive1.log.gz and testarchive2.log.gz', function () {
for (var num = 0; num < 4; num++) {
const file = !num ? 'testarchive.log' : 'testarchive' + num + '.log.gz';
const fullpath = path.join(__dirname, '..', 'fixtures', 'logs', file);

if (num === 3) {
return assert.throws(function () {
fs.statSync(fullpath);
}, Error);
}

assert.doesNotThrow(function () {
fs.statSync(fullpath);
}, Error);
}
});
});
});

0 comments on commit ed27e85

Please sign in to comment.