winstonjs / winston-daily-rotate-file

A transport for winston which logs to a rotating file each day.
MIT License
899 stars 156 forks source link

Add Storage Configuration #392

Open chenyulun opened 8 months ago

chenyulun commented 8 months ago

Can you realize that the current log is written to a file without a date, and the next day the file is dumped to a file with a date for backup, which is not the same as creating createSymlink?

Because our file-listening service (the log-collecting service) doesn't support createSymlink and can only listen to a single file

Apollon77 commented 8 months ago

The current fikle which is there as symlink is not sufficient?

chenyulun commented 8 months ago

我们的监听日志文件是用java写的,可能没有配置symlink的支持,我也fork了项目,添加了fixed_name,在创建文件的时候和备份日期的时候做了差异 file-stream-rotator/src/FileStreamRotator.ts

export default class FileStreamRotator extends EventEmitter {
  rotate(force: boolean = false) {
    let oldFile = this.currentFile;
    this.rotator.rotate(force);
    this.currentFile = this.rotator.getNewFilename();
    let _filename = this.currentFile;
    if (this.config.options?.fixed_name) {
      let logPath = path.dirname(this.currentFile);
      _filename = logPath + path.sep + this.config.options?.fixed_name;
    }

    // oldfile same as new file. do nothing
    if (this.currentFile == oldFile) {
      return;
    }

    // close old file and watcher if exists.
    if (this.fs) {
      // if (this.logWatcher) {
      //     this.logWatcher.close()
      // }
      if (this.config.options?.end_stream === true) {
        this.fs.end();
      } else {
        this.fs.destroy();
      }
      if (oldFile && this.config.options?.fixed_name) {
        this.saveOldLog(oldFile, _filename);
      }
    }

    // add old file to audit
    if (oldFile) {
      this.auditManager.addLog(oldFile);
    }

    this.createNewLog(_filename);
    this.emit('new', _filename);
    this.emit('rotate', oldFile, _filename, force);
  }

  private saveOldLog(saveFilename: string, filename: string) {
    makeDirectory(saveFilename);
    fs.copyFileSync(filename, saveFilename);
  }
}
Apollon77 commented 8 months ago

@chenyulun Can you please write in english?

chenyulun commented 8 months ago

Our listening log file is written in java, may not have configured symlink support, I also forked the project, added fixed_name, in the creation of the file and the backup date to do the differences