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

dateFile misleading parameter daysToKeep #1080

Closed
chienfuchen32 opened this issue May 20, 2021 · 2 comments · Fixed by log4js-node/streamroller#78 or #1135
Closed

dateFile misleading parameter daysToKeep #1080

chienfuchen32 opened this issue May 20, 2021 · 2 comments · Fixed by log4js-node/streamroller#78 or #1135
Milestone

Comments

@chienfuchen32
Copy link

chienfuchen32 commented May 20, 2021

Hi, I've tried to use log4js dateFile recently.

daysToKeep - integer (default 0) - if this value is greater than zero, then files older than that many days will be deleted during log rolling.

dateFile Document

When I test a logger like the example,
after a while, its behavior like rotating with numbers of archives.
Then I check the source code of dateFile, it implement DateRollingFileStream form streamroller,

and I look into DateRollingFileStream,

class DateRollingFileStream extends RollingFileWriteStream {
  constructor(filename, pattern, options) {
    //...
    if (options.daysToKeep) {
      options.numToKeep = options.daysToKeep;
    }
}

DateRollingFileStream inherit from RollingFileWriteStream which provide cleaning up mechanism based on numToKeep

class RollingFileWriteStream extends Writable {
  //...
  // Sorted from the oldest to the latest
  async _getExistingFiles() {
    const files = await fs.readdir(this.fileObject.dir).catch(() => []);

    debug(`_getExistingFiles: files=${files}`);
    const existingFileDetails = files
      .map(n => this.fileNameParser(n))
      .filter(n => n);

    const getKey = n =>
      (n.timestamp ? n.timestamp : newNow().getTime()) - n.index;
    existingFileDetails.sort((a, b) => getKey(a) - getKey(b));

    return existingFileDetails;
  }
  //...
  async _clean() {
    const existingFileDetails = await this._getExistingFiles();
    debug(
      `_clean: numToKeep = ${this.options.numToKeep}, existingFiles = ${existingFileDetails.length}`
    );
    debug("_clean: existing files are: ", existingFileDetails);
    if (this._tooManyFiles(existingFileDetails.length)) {
      const fileNamesToRemove = existingFileDetails
        .slice(0, existingFileDetails.length - this.options.numToKeep - 1)
        .map(f => path.format({ dir: this.fileObject.dir, base: f.filename }));
      await deleteFiles(fileNamesToRemove);
    }
  }
}

I think it's kind of confusing documentation... or if there's a plan that streamroller implement file rotation based on file modified time or datetime naming rule in the future?
Thank you. :D

@orbulant
Copy link

I agree, "daysToKeep' should actually be renamed to "filesToKeep".

@lamweili
Copy link
Contributor

lamweili commented Jan 19, 2022

It's renamed to numBackups instead for clarity while daysToKeep is still backward compatible.
Usage of daysToKeep will result in process.emitWarning of deprecation.

https://github.com/log4js-node/streamroller/blob/272e5e32e742021a4340ab147f7bea24c8924ddd/lib/DateRollingFileStream.js#L21-L25

process.emitWarning(
  "options.daysToKeep is deprecated due the confusion it causes when used " + 
  "together with file size rolling. Please use options.numBackups instead.",
  "DeprecationWarning", "StreamRoller0001"
);

aocm added a commit to aocm/vue3-express-ssr-sample that referenced this issue Apr 15, 2022
log4js-node/log4js-node#1080
```
(node:38719) [streamroller-DEP0001] DeprecationWarning: options.daysToKeep is deprecated due to the confusion it causes when used together with file size rolling. Please use options.numBackups instead.
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants