Skip to content

After restart server, last time's newest log file is not archived and oldest log file is not deleted #296

@k872892

Description

@k872892

Description

After restart node server:

  1. The last time's newest log file is not archived.

  2. The last time's oldest archived log file is not deleted when exceed maxFiles.

Version

4.5.0

Log settings

var transport = new winston.transports.DailyRotateFile({
    filename: './logs/application-%DATE%.log',
    datePattern: 'YYYY-MM-DD-HH-mm:ss',
    zippedArchive: true,
    maxFiles: 5 ,
    maxSize: '1k'
});

Test steps

  1. Use test script to generate 5 log files (one file per second).
node test.js 5
  1. Check log files
Pin@ubuntu # ll -t -r logs/
-rw-r--r--    1 root     root            69 Oct 12 18:23 application-2020-10-12-18-23:01.log.gz
-rw-r--r--    1 root     root            69 Oct 12 18:23 application-2020-10-12-18-23:02.log.gz
-rw-r--r--    1 root     root            69 Oct 12 18:23 application-2020-10-12-18-23:03.log.gz
-rw-r--r--    1 root     root            50 Oct 12 18:23 application-2020-10-12-18-23:05.log
-rw-r--r--    1 root     root            69 Oct 12 18:23 application-2020-10-12-18-23:04.log.gz
-rw-r--r--    1 root     root          1039 Oct 12 18:23 .4fdb28c558b05eafd9ee9c1d1660cb8f60f66f53-audit.json
  1. Use test script to generate 3 log files (one file per second).
node test.js 3

4 Check log files

Pin@ubuntu # ll -t -r logs/
-rw-r--r--    1 root     root            69 Oct 12 18:23 application-2020-10-12-18-23:01.log.gz
-rw-r--r--    1 root     root            50 Oct 12 18:23 application-2020-10-12-18-23:05.log
-rw-r--r--    1 root     root            20 Oct 12 18:23 application-2020-10-12-18-23:08.log.gz
-rw-r--r--    1 root     root            69 Oct 12 18:23 application-2020-10-12-18-23:09.log.gz
-rw-r--r--    1 root     root            50 Oct 12 18:23 application-2020-10-12-18-23:11.log
-rw-r--r--    1 root     root            69 Oct 12 18:23 application-2020-10-12-18-23:10.log.gz
-rw-r--r--    1 root     root          1039 Oct 12 18:23 .4fdb28c558b05eafd9ee9c1d1660cb8f60f66f53-audit.json

Expected:

  1. application-2020-10-12-18-23:05.log should be archived
  2. application-2020-10-12-18-23:01.log.gz should be deleted

Test Script

test.js

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

var transport = new winston.transports.DailyRotateFile({
    filename: './logs/application-%DATE%.log',
    datePattern: 'YYYY-MM-DD-HH-mm:ss',
    zippedArchive: true,
    maxFiles: 5 ,
    maxSize: '1k'
});

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

function makeid(length) {
   var result           = '';
   var characters       = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
   var charactersLength = characters.length;
   for ( var i = 0; i < length; i++ ) {
      result += characters.charAt(Math.floor(Math.random() * charactersLength));
   }
   return result;
}

function waitAndDo(times) {
    if (times < 1) {
        return;
    }

    setTimeout(function() {
        logger.info(makeid(20));

        waitAndDo(times-1);
    }, 1000);
}

waitAndDo(process.argv[2]);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions