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

Two issues if zippedArchive is true #71

Closed
Konni1337 opened this issue Feb 2, 2017 · 1 comment
Closed

Two issues if zippedArchive is true #71

Konni1337 opened this issue Feb 2, 2017 · 1 comment

Comments

@Konni1337
Copy link

There are two different issues I encountered with the file compression:

  1. The old file doesn't get compressed if there is only one log entry. The problem seems to be that the archive is not set when the stream is created.
  2. After the third file that is being generated, the new file gets the counter appended to the filename, even when the new filename is unique.

I wrote two tests to demonstrate the above mentioned problems:

describe('when zippedArchive is true', function () {
  var pattern = {
    pattern: '.m',
    start: 1861947160000, // GMT: Mon, 01 Jan 2029 07:32:50 GMT
    mid: 1861947240000, // GMT: Mon, 01 Jan 2029 07:34:00 GMT
    end: 1861947760000, // GMT: Mon, 01 Jan 2029 07:42:40 GMT
    oldfile: 'test-rotation.log.32',
    midfile: 'test-rotation.log.34',
    newfile: 'test-rotation.log.42'
  };

  var transport;
  var rotationLogPath = path.join(fixturesDir, 'rotations');
  beforeEach(function (done) {
    this.time = new Date(pattern.start);
    tk.travel(this.time);
    rimraf.sync(rotationLogPath);
    mkdirp.sync(rotationLogPath);
    transport = new DailyRotateFile({
      filename: path.join(rotationLogPath, 'test-rotation.log'),
      datePattern: pattern.pattern,
      zippedArchive: true
    });

    done();
  });

  afterEach(function () {
    tk.reset();
  });

  it('should compress logfile even if there is only one log message', function (done) {

    var self = this;

    transport.log('error', 'test message', {}, function (err) {
      if (err) {
        done(err);
      }

      var filesCreated = fs.readdirSync(rotationLogPath);
      expect(filesCreated.length).to.eql(1);
      expect(filesCreated).to.include(pattern.oldfile);
      self.time = new Date(pattern.end);
      tk.travel(self.time);
      transport.log('error', '2nd test message', {}, function (err) {
        if (err) {
          done(err);
        }

        filesCreated = fs.readdirSync(rotationLogPath);
        expect(filesCreated.length).to.eql(2);
        expect(filesCreated).to.include(pattern.newfile);
        expect(filesCreated).to.include(pattern.oldfile + '.gz');
        transport.close();

        done();
      });
    });
  });

  it('should compress logfile without adding count to file extension if there are no files with the same name', function (done) {

    var self = this;

    transport.log('error', 'test message', {}, function (err) {
      if (err) {
        done(err);
      }
      transport.log('error', 'test message', {}, function (err) {
        if (err) {
          done(err);
        }

        var filesCreated = fs.readdirSync(rotationLogPath);
        expect(filesCreated.length).to.eql(1);
        expect(filesCreated).to.include(pattern.oldfile);
        self.time = new Date(pattern.mid);
        tk.travel(self.time);
        transport.log('error', '2nd test message', {}, function (err) {
          if (err) {
            done(err);
          }
          transport.log('error', 'test message', {}, function (err) {
            if (err) {
              done(err);
            }

            filesCreated = fs.readdirSync(rotationLogPath);
            expect(filesCreated.length).to.eql(2);
            expect(filesCreated).to.include(pattern.oldfile + '.gz');
            expect(filesCreated).to.include(pattern.midfile);
            self.time = new Date(pattern.end);
            tk.travel(self.time);
            transport.log('error', '3rd test message', {}, function (err) {
              if (err) {
                done(err);
              }
              transport.log('error', 'test message', {}, function (err) {
                if (err) {
                  done(err);
                }

                filesCreated = fs.readdirSync(rotationLogPath);
                expect(filesCreated.length).to.eql(3);
                expect(filesCreated).to.not.include(pattern.newfile + '.1');
                expect(filesCreated).to.include(pattern.newfile);
                expect(filesCreated).to.include(pattern.midfile + '.gz');
                expect(filesCreated).to.include(pattern.oldfile + '.gz');
                transport.close();

                done();
              });
            });
          });
        });
      });
    });
  });
});`
@mattberther
Copy link
Member

Thanks for your unit tests, @Konni1337. #74 and b626d08 have been merged which should close this out.

Published 1.4.5 to npm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants