Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added functionality to long broken zippedArchive option #2337

Merged
merged 13 commits into from
Jan 3, 2024
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