如何使用winston logger追加日期和时间?

时间:2017-03-21 19:41:59

标签: javascript node.js logging winston

我有winston logger将事件记录到logs/server.log,当它旋转文件时由于日期格式而创建server20170321114031.log但我想以这种格式server.log.20170321114031旋转文件并且当前记录文件名应始终为server.log是否可以使用winston实现?

winston.js

logger = new (winston.Logger)({
            level: null,
            transports: [
//                new (winston.transports.Console)(),
                new (winston.transports.File)({
                    filename: './logs/server.log',
                    maxsize: 1024 * 1024 * 20,//15728640 is 15 MB
                    timestamp: false,
                    json: false,
                    rotationFormat: function() {
                        return getFormattedDate();
                        function getFormattedDate() {
                          var temp = new Date();
                          return dateStr = padStr(temp.getFullYear()) +
                          padStr(1 + temp.getMonth()) +
                          padStr(temp.getDate()) +
                          padStr(temp.getHours()) +
                          padStr(temp.getMinutes()) +
                          padStr(temp.getSeconds());
                        }
                        function padStr(i) {
                          return (i < 10) ? "0" + i : "" + i;
                        }
                      },
                    formatter: function (options) {
                        return options.message;
                    }
                })
            ]
        });

1 个答案:

答案 0 :(得分:1)

请参阅https://github.com/winstonjs/winston/pull/205

logger.configure({
  category1: {
    dailyRotateFile: {
      colorize: 'true',
      filename: 'dailyRotateFile.log',
      datePattern: '.yyyy-MM-dd',
      maxsize: 20000
    }
  }
});