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 @@
return;
}
if (this.lazy) {
this._endStream(() => {this.emit('fileclosed')});
this._endStream(() => {this.emit('fileclosed');});
return;
}

Expand Down Expand Up @@ -297,7 +297,7 @@
if (callback) callback(null, results);
});

function add(buff, attempt) {

Check warning on line 300 in lib/winston/transports/file.js

View workflow job for this annotation

GitHub Actions / Tests (16)

'buff' is already declared in the upper scope on line 254 column 9

Check warning on line 300 in lib/winston/transports/file.js

View workflow job for this annotation

GitHub Actions / Tests (18)

'buff' is already declared in the upper scope on line 254 column 9

Check warning on line 300 in lib/winston/transports/file.js

View workflow job for this annotation

GitHub Actions / Tests (20)

'buff' is already declared in the upper scope on line 254 column 9
try {
const log = JSON.parse(buff);
if (check(log)) {
Expand Down Expand Up @@ -358,7 +358,7 @@
return true;
}

function normalizeQuery(options) {

Check warning on line 361 in lib/winston/transports/file.js

View workflow job for this annotation

GitHub Actions / Tests (16)

'options' is already declared in the upper scope on line 246 column 9

Check warning on line 361 in lib/winston/transports/file.js

View workflow job for this annotation

GitHub Actions / Tests (18)

'options' is already declared in the upper scope on line 246 column 9

Check warning on line 361 in lib/winston/transports/file.js

View workflow job for this annotation

GitHub Actions / Tests (20)

'options' is already declared in the upper scope on line 246 column 9
options = options || {};

// limit
Expand Down Expand Up @@ -603,12 +603,6 @@
});

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 @@
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 @@
// 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 @@

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
Loading