Rcomian / bunyan-rotating-file-stream

Is an endpoint for bunyan that lets you control how much disk space logs take.
Other
29 stars 15 forks source link

Race condition while rotating logs: EPERM: operation not permitted, stat #19

Closed rdbr closed 3 years ago

rdbr commented 6 years ago

I have been using the rotating file stream for a long time successfully, but I think there is a race condition when files are getting rotated and zipped.

My application crashed with the following error: Error: EPERM: operation not permitted, stat 'C:\path\to\log\test-2018-04-09.log'

The directory shows that C:\path\to\log\test-2018-04-09.log.gz was created successfully and the test-2018-04-09.log was deleted. It should not have tried to access the old, deleted, log file.

Below is my init code. I didn't receive an error event, it just crashed.

    // Create logger
    this.logger = bunyan.createLogger({
      name: 'test',
      streams: [{
        type: 'raw',
        level: 'info',
        stream: new RotatingFileStream({
          path: filePath + '-%Y-%m-%d-%N.log',
          threshold: '350m',
          gzip: true
        })
      }]
    });

    this.logger.on('error', (error, stream) => {
      console.log('Logger error: ' + error.message);
    });
rdbr commented 6 years ago

Maybe it is a different problem. Could it be a naming conflict if I have two loggers with these names, so the prefix includes a -: test-%Y-%m-%d-%N.log test-test2-%Y-%m-%d-%N.log

During rotation, maybe the wrong file is rotated.

Rcomian commented 6 years ago

Sorry I've not had any time to look at these issues recently.

But yes, it's definitely possible that one or other of the loggers is picking up those files as its own. I'm not 100% sure, but if you could change the prefixes, to something definitely unambiguous, it should work ok.

There might also be a problem even if that's not the case, I'm not sure if we stat all the files in the directory regardless of their name - if we do that, then there's a definite race condition. I'll try to have a look, and hopefully fix that up.

Rcomian commented 3 years ago

Ok looking at this, for sure it's that the logger for test-%Y-%m-%d-%N.log is looking for all files starting test- and ending with .log, which will include the test-test2 files. If you gave them unique prefixes, eg test-test1 & test-test2, then this should fix the issue.

Apologies for the delay, I had some personal issues and this project suffered as a result, nothing to do with the project itself at all.