Ограничение файлов по MaxFiles не работает, когда я делаю zippedArchive: правда, используя winston - PullRequest
0 голосов
/ 04 июня 2019

Цель : мы планируем ежедневно архивировать файлы, хранить резервную копию в течение 10 дней и удалять старые файлы

код

var appRoot = require('app-root-path');
const fs = require('fs');
var { createLogger, format, transports } = require('winston');
require('winston-daily-rotate-file');
var { combine, timestamp, label, printf } = format;
const appId = fs.readFileSync(`/home/qolsys/logRestApp/myid`, 'utf8');

// define the custom settings for each transport (file, console)

const dailyRotateFileTransport = new transports.DailyRotateFile({
   filename: `/home/logRestApp/logs/%DATE%-app.log`,
   datePattern: 'YYYY-MM-DD-HH-mm',
   maxsize: 1048576, // 1MB
   maxFiles: 3,
   zippedArchive: true,
   colorize: false,
   tailable: true,
   prettyPrint: true
});

var options = {
  file: {
    level: 'info',
    name: 'file.info',
    filename: `/home/logRestApp/logs/app.log`,
    handleExceptions: true,
    json: true,
    colorize: false,
    maxFiles: 1,
    tailable: true,
    prettyPrint: true
  },
  console: {
    level: 'debug',
    handleExceptions: true,
    json: false,
    colorize: true,
    timestamp: true,
    tailable: true,
    prettyPrint: true
  },
};


  dailyRotateFileTransport.on('rotate', function(oldFilename, newFilename) {

  });


var pattern = format.combine(
   format.label({ label: 'ns'+ appId }),
   format.timestamp({format: 'YYYY-MM-DD HH:mm:ss:SSS'}),
   format.printf(info => `${info.timestamp}*[${info.level}]*${info.label}*${info.message}`),
   format.splat(),
   // format.json(),
   format.colorize()
 );

// instantiate a new Winston Logger with the settings defined above

var logger = createLogger({
 format: pattern,
 // format: format.combine(format.timestamp({format: 'YYYY-MM-DD HH:mm:ss'}), format.colorize(), format.simple()),
 transports: [
   new transports.File(options.file),
   new transports.Console(options.console),
   dailyRotateFileTransport
 ],
 exitOnError: false, // do not exit on handled exceptions
 silent: false,
 meta: false
});

// create a stream object with a 'write' function that will be used by `morgan`
logger.stream = {
 write: function(message, encoding) {
   // use the 'info' log level so the output will be picked up by both transports (file and console)
   logger.info(message);
 },
};

module.exports = logger;

рабочие контрольные примеры:

мы протестировали конфигурацию, в которой мы каждую минуту кидаем файл журнала, сохраняем резервную копию в течение 3 минут и удаляем старый файл, когда файл на 4-й минуте создан и работает. удаление старых файлов после лимита.

посмотрите на скриншот:

working_for_log_files

НЕ работает тестовый набор: ограничение на количество файлов не работает, когда мы архивируем журналы, старые файлы не удаляются, когда он достигает предела maxfiles

взгляните на скриншот: not_working_gzip_files

...