Skip to content

dateFile misleading parameter daysToKeep #1080

Closed
log4js-node/streamroller
#78

Description

@chienfuchen32

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

Activity

  1. orbulant commented on Sep 30, 2021

    @orbulant

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

  2. added this to the 6.4.0 milestone on Jan 11, 2022
  3. lamweili commented on Jan 19, 2022

    @lamweili
    Contributor

    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"
    );
  4. added a commit that references this issue on Apr 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions