Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete files from winston log after certain days limit?

I am using winston to log files into server that is working as expected , now i want to set days limit , Lets say after 3 days i want to delete files that are logged 3 days ago , is it possible to achieve using winston rotation ?

main.js

 winston.add(winston.transports.File, {
        filename: './Logs/server.log',
        maxsize:'15000',
        timestamp:false
    });
like image 895
hussain Avatar asked Jul 08 '16 18:07

hussain


People also ask

How do I clean up log files?

Command Prompt Log Clearing When the Command Prompt window opens, type the command "cd" (without quotes) and press "Enter," and then type "cd windows" before pressing "Enter" once more. You can then enter the command "del *. log /a /s /q /f" and press "Enter" to delete all log files from the Windows directory.

How do I change the log level in Winston?

You can change the logging level in runtime by modifying the level property of the appropriate transport: var log = new (winston. Logger)({ transports: [ new (winston. transports.

How do I view Winston logs?

Go to the example. log file in the logs folder to view the log. Winston allows you to implement multiple logging transports, i.e., a log can be recorded to a file, console, or database. The Logger configuration below logs to a console and a file.


1 Answers

You can use Winston daily rotate for this.

Short answer: Yes, you can do that by adding maxFiles:3d in transport option.

  • If you do not set the maxFiles, no log file will be deleted.

  • If you set maxFiles:3d, it will delete all log files created in the last 3 days.

  • If you set maxFiles:3, it will delete log files when it exceeds more than 3.

Note: You can archive files before deleting by using zippedArchive: true, if not set the logs files will be deleted without any backup. Till now, I didn't get any way to auto-delete zip files.

Sample setup:

  require('winston-daily-rotate-file');

  var transport = new (winston.transports.DailyRotateFile)({
    filename: 'application-%DATE%.log',
    datePattern: 'YYYY-MM-DD-HH',
    zippedArchive: true,
    maxSize: '20m',
    maxFiles: '14d'
  });

  transport.on('rotate', function(oldFilename, newFilename) {
    // do something fun
  });

  var logger = winston.createLogger({
    transports: [
      transport
    ]
  });

  logger.info('Hello World!');```


like image 66
Neo Avatar answered Oct 23 '22 22:10

Neo