Skip to content

Commit 02d4267

Browse files
Pulkit0729myAlapiDABH
authored
Added functionality to long broken zippedArchive option (#2337)
* Test for file Transport option rotationFormat * Test for file Transport option rotationFormat * Added Lazy option in file transport * Lint and test * removed only statement * Update test/unit/winston/transports/01-file-maxsize.test.js Co-authored-by: David Hyde <[email protected]> * Update test/unit/winston/transports/01-file-maxsize.test.js Co-authored-by: David Hyde <[email protected]> * Added lazy in FileTransportOptions types * Added lazy type in FileTransportInstance * Add functionality to zippedArchive option --------- Co-authored-by: myAlapi <[email protected]> Co-authored-by: David Hyde <[email protected]>
1 parent 069a40d commit 02d4267

File tree

2 files changed

+365
-18
lines changed

2 files changed

+365
-18
lines changed

lib/winston/transports/file.js

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ module.exports = class File extends TransportStream {
198198
return;
199199
}
200200
if (this.lazy) {
201-
this._endStream(() => {this.emit('fileclosed')});
201+
this._endStream(() => {this.emit('fileclosed');});
202202
return;
203203
}
204204

@@ -603,12 +603,6 @@ module.exports = class File extends TransportStream {
603603
});
604604

605605
debug('create stream ok', fullpath);
606-
if (this.zippedArchive) {
607-
const gzip = zlib.createGzip();
608-
gzip.pipe(dest);
609-
return gzip;
610-
}
611-
612606
return dest;
613607
}
614608

@@ -621,13 +615,33 @@ module.exports = class File extends TransportStream {
621615
debug('_incFile', this.filename);
622616
const ext = path.extname(this._basename);
623617
const basename = path.basename(this._basename, ext);
618+
const tasks = [];
624619

625-
if (!this.tailable) {
626-
this._created += 1;
627-
this._checkMaxFilesIncrementing(ext, basename, callback);
628-
} else {
629-
this._checkMaxFilesTailable(ext, basename, callback);
620+
if (this.zippedArchive) {
621+
tasks.push(
622+
function (cb) {
623+
const num = this._created > 0 && !this.tailable ? this._created : '';
624+
this._compressFile(
625+
path.join(this.dirname, `${basename}${num}${ext}`),
626+
path.join(this.dirname, `${basename}${num}${ext}.gz`),
627+
cb
628+
);
629+
}.bind(this)
630+
);
630631
}
632+
633+
tasks.push(
634+
function (cb) {
635+
if (!this.tailable) {
636+
this._created += 1;
637+
this._checkMaxFilesIncrementing(ext, basename, cb);
638+
} else {
639+
this._checkMaxFilesTailable(ext, basename, cb);
640+
}
641+
}.bind(this)
642+
);
643+
644+
asyncSeries(tasks, callback);
631645
}
632646

633647
/**
@@ -646,13 +660,9 @@ module.exports = class File extends TransportStream {
646660
// Caveat emptor (indexzero): rotationFormat() was broken by design When
647661
// combined with max files because the set of files to unlink is never
648662
// stored.
649-
const target = !this.tailable && this._created
663+
return !this.tailable && this._created
650664
? `${basename}${isRotation}${ext}`
651665
: `${basename}${ext}`;
652-
653-
return this.zippedArchive && !this.tailable
654-
? `${target}.gz`
655-
: target;
656666
}
657667

658668
/**
@@ -715,13 +725,36 @@ module.exports = class File extends TransportStream {
715725

716726
asyncSeries(tasks, () => {
717727
fs.rename(
718-
path.join(this.dirname, `${basename}${ext}`),
728+
path.join(this.dirname, `${basename}${ext}${isZipped}`),
719729
path.join(this.dirname, `${basename}1${ext}${isZipped}`),
720730
callback
721731
);
722732
});
723733
}
724734

735+
/**
736+
* Compresses src to dest with gzip and unlinks src
737+
* @param {string} src - path to source file.
738+
* @param {string} dest - path to zipped destination file.
739+
* @param {Function} callback - callback called after file has been compressed.
740+
* @returns {undefined}
741+
* @private
742+
*/
743+
_compressFile(src, dest, callback) {
744+
fs.access(src, fs.F_OK, (err) => {
745+
if (err) {
746+
return callback();
747+
}
748+
var gzip = zlib.createGzip();
749+
var inp = fs.createReadStream(src);
750+
var out = fs.createWriteStream(dest);
751+
out.on('finish', () => {
752+
fs.unlink(src, callback);
753+
});
754+
inp.pipe(gzip).pipe(out);
755+
});
756+
}
757+
725758
_createLogDirIfNotExist(dirPath) {
726759
/* eslint-disable no-sync */
727760
if (!fs.existsSync(dirPath)) {

0 commit comments

Comments
 (0)