Skip to content

Commit

Permalink
Added functionality to long broken zippedArchive option (#2337)
Browse files Browse the repository at this point in the history
* 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 <DABH@users.noreply.github.com>

* Update test/unit/winston/transports/01-file-maxsize.test.js

Co-authored-by: David Hyde <DABH@users.noreply.github.com>

* Added lazy in FileTransportOptions types

* Added lazy type in FileTransportInstance

* Add functionality to zippedArchive option

---------

Co-authored-by: myAlapi <myalapi2022@gmail.com>
Co-authored-by: David Hyde <DABH@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 3, 2024
1 parent 069a40d commit 02d4267
Show file tree
Hide file tree
Showing 2 changed files with 365 additions and 18 deletions.
69 changes: 51 additions & 18 deletions lib/winston/transports/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ module.exports = class File extends TransportStream {
return;
}
if (this.lazy) {
this._endStream(() => {this.emit('fileclosed')});
this._endStream(() => {this.emit('fileclosed');});
return;
}

Expand Down Expand Up @@ -603,12 +603,6 @@ module.exports = class File extends TransportStream {
});

debug('create stream ok', fullpath);
if (this.zippedArchive) {
const gzip = zlib.createGzip();
gzip.pipe(dest);
return gzip;
}

return dest;
}

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

if (!this.tailable) {
this._created += 1;
this._checkMaxFilesIncrementing(ext, basename, callback);
} else {
this._checkMaxFilesTailable(ext, basename, callback);
if (this.zippedArchive) {
tasks.push(
function (cb) {
const num = this._created > 0 && !this.tailable ? this._created : '';
this._compressFile(
path.join(this.dirname, `${basename}${num}${ext}`),
path.join(this.dirname, `${basename}${num}${ext}.gz`),
cb
);
}.bind(this)
);
}

tasks.push(
function (cb) {
if (!this.tailable) {
this._created += 1;
this._checkMaxFilesIncrementing(ext, basename, cb);
} else {
this._checkMaxFilesTailable(ext, basename, cb);
}
}.bind(this)
);

asyncSeries(tasks, callback);
}

/**
Expand All @@ -646,13 +660,9 @@ module.exports = class File extends TransportStream {
// Caveat emptor (indexzero): rotationFormat() was broken by design When
// combined with max files because the set of files to unlink is never
// stored.
const target = !this.tailable && this._created
return !this.tailable && this._created
? `${basename}${isRotation}${ext}`
: `${basename}${ext}`;

return this.zippedArchive && !this.tailable
? `${target}.gz`
: target;
}

/**
Expand Down Expand Up @@ -715,13 +725,36 @@ module.exports = class File extends TransportStream {

asyncSeries(tasks, () => {
fs.rename(
path.join(this.dirname, `${basename}${ext}`),
path.join(this.dirname, `${basename}${ext}${isZipped}`),
path.join(this.dirname, `${basename}1${ext}${isZipped}`),
callback
);
});
}

/**
* Compresses src to dest with gzip and unlinks src
* @param {string} src - path to source file.
* @param {string} dest - path to zipped destination file.
* @param {Function} callback - callback called after file has been compressed.
* @returns {undefined}
* @private
*/
_compressFile(src, dest, callback) {
fs.access(src, fs.F_OK, (err) => {
if (err) {
return callback();
}
var gzip = zlib.createGzip();
var inp = fs.createReadStream(src);
var out = fs.createWriteStream(dest);
out.on('finish', () => {
fs.unlink(src, callback);
});
inp.pipe(gzip).pipe(out);
});
}

_createLogDirIfNotExist(dirPath) {
/* eslint-disable no-sync */
if (!fs.existsSync(dirPath)) {
Expand Down
Loading

0 comments on commit 02d4267

Please sign in to comment.