metarhia/metalog

using tee for redirection leads to error

Closed this issue · 1 comments

Hello,

Have made mocha tests for metalog.
Once running tests like this:

mocha metalogtest.js | tee 1

I get this error:

  1) metalog
       init:
     Uncaught TypeError: Cannot read property 'stack' of null
      at fs.unlink.err (/home/someuser/Downloads/tests/node_modules/metalog/metalog.js:180:41)
      at FSReqWrap.args [as oncomplete] (fs.js:140:20)

Another note that forwarding output streams using tee or &> 1 doesn't work either, the 1 output doesn't get created.

Here is quick fix:
################

  rotate() {
    if (!this.keepDays) return;
    fs.readdir(this.path, (err, files) => {
      if (err) {
        process.stdout.write(`${err.stack}\n`);
        this.emit('error', err);
        return;
      }
      const now = new Date();
      const year = now.getUTCFullYear();
      const month = now.getUTCMonth();
      const day = now.getUTCDate();
      const date = new Date(year, month, day, 0, 0, 0, 0);
      const time = date.getTime();
      for (const fileName of files) {
        const fileTime = new Date(fileName.substring(0, 10)).getTime();
        const fileAge = Math.floor((time - fileTime) / DAY_MILLISECONDS);
        if (fileAge > 1 && fileAge > this.keepDays - 1) {
          fs.unlink(this.path + '/' + fileName, err => {
if (err) { // QUICK FIX
            process.stdout.write(`${err.stack}\n`);
            this.emit('error', err);
}
          });
        }
      }
    });
  }

####################################
Here is my simple test itself:

describe('metalog', ()=>{
    let logger;

    after(()=>{
        logger.close();
    });

    it('init', ()=>{
        const metalog = require('metalog');

        const config = {
            enabled: true,
            keepDays: 100, // Delete files after N days
            writeInterval: 3000,//Duration('3s'), // Flush log to disk interval
            writeBuffer: 64 * 1024, // Buffer size 64kb
            toStdout: ['system', 'fatal', 'error']
        };

        const { writeInterval, writeBuffer, keepDays, toStdout, toFile } = config;

        const logDir = process.cwd();
        const nodeId = 'N1';
        logger = metalog({
            path: logDir,
            node: nodeId,
            writeInterval,
            writeBuffer,
            keepDays,
            toStdout,
            toFile
        });
    });

    it('system', ()=>{
        logger.system('1.system');
    });
});

Fixed in #112