Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS - WinstonJS - Clear the log file

I'm using WinstonJS for logging to a file, and nodemon to restart when I update my code.

var winston = require('winston');

var logger = new (winston.Logger)({
    transports: [
        new (winston.transports.File)({
            level: 'silly',
            filename: __dirname + '/logs/test.log',
            json: false
        })
    ]
});

logger.log('info', 'something');

The log file is being appended to, however when I make a code change and save my file, nodemon runs it again and the log file is appended to again. This leads to a log file that just keeps getting longer and longer, and I have to keep manually deleting the contents.

I guess I could do something with the fs module, but it would be far nicer to use Winston to say something like update: replace|append, but I can't see anything like that available.

Is there any way to clear the log file, so I only have log entries for the last time my code was run..?

like image 865
Stephen Last Avatar asked Dec 16 '15 16:12

Stephen Last


2 Answers

The

{ flags: 'w' }

property on the options object on the new transport is working for me:

  transports: [
    new winston.transports.File({ filename: 'myLog.txt', options: { flags: 'w' } })
  ]
like image 110
MattG Avatar answered Oct 09 '22 03:10

MattG


I know this question is a little outdated, but I recently found the answer to this and thought I would share it here for posterity. You can pass fs.createWriteStream options to the File logger:

// Open the file in "write" mode, which truncates first
options: { flags: 'w' },
like image 24
Chad Robinson Avatar answered Oct 09 '22 04:10

Chad Robinson