如何在使用winston在Node JS中达到特定大小限制后归档日志文件?

时间:2016-03-02 14:43:18

标签: node.js express logging archive winston

我是Node JS的新手。我正在寻找Node的日志库,我找到了Winston。现在,我希望在日志文件达到特定大小时将其归档,并在名称中包含时间戳。

例如:

当前日志文件:logs / devlog.log(10 MB)

因为它达到10 MB,我想将其归档

归档日志文件:Archive / devlog-03.02.2016.log

请帮助我在winston或任何其他解决方案中这样做。提前致谢

我的app.js代码(节点服务器):

    console.log('Hello world');
var winston = require('winston');
var date1 = new Date();
winston.emitErrs = true;
var date = new Date();
var loggerName = "server.app.js";
var winston = require('winston'),
path = require('path'),
transports = [];

transports.push(new winston.transports.DailyRotateFile({
    name: 'file',
    datePattern: '.yyyy-MM-ddTHH-mm',
    filename: path.join(__dirname, "logs", "log_file.log"),
    maxFiles: 5,
    maxsize: 100000000
}));

var logger = new winston.Logger({ transports: transports });
//winston.loggers.add('ServerLogger', {
//    transports: [
//        new (winston.transports.File)({
//            name: 'Devlogger', //Name of the transport
//            filename: 'logs/devlogNormalF.log', // log file name
//            json: false,
//            maxsize: 1048576, //1MB
//            maxFiles: 10, // 10 Files max
//            timestamp: function () {
//                return new Date().toISOString();//.substring(0,23);
//            }
//        }
//        ), new archiveFile(options)
//    ]
//});
//var logger = winston.loggers.get('ServerLogger');
logger.info ('Logger Name: '+loggerName+' App starting...');
logger.info('Logger Name: ' + loggerName + 'Hello world');
logger.info('Logger Name: ' + loggerName + ' Accessing test/user');
logger.error('Logger Name: ' + loggerName + ' Cannot access test/user/user.png');
var express = require('express');
var app = express();
app.get('/', function (req, res) {
    res.send('Hello World!');
    var date2 = new Date();
    var diff = date2 - date1;
    logger.info('Start '+ date1+' Stop '+date2)
    logger.info('Logger Name: ' + loggerName + ' Response time of Node is ' + diff+' ms');
});

app.listen(3000, function () {
    logger.warn('Logger Name: ' + loggerName + ' Example app listening on port 3000!');
    logger.info('Logger Name: ' + loggerName + ' App ending...');
});

1 个答案:

答案 0 :(得分:1)

Winston使用日期模式支持日志 您可以像这样指定datepattern,maxfilesize和maxfile:

var winston     = require ('winston'),
    path        = require ('path'),
    transports  = [];

transports.push(new winston.transports.DailyRotateFile({
  name: 'file',
  datePattern: '.yyyy-MM-ddTHH-mm',
  filename: path.join(__dirname, "logs", "log_file.log"),
  maxFiles: 5,
  maxsize: 100000000

}));

var logger = new winston.Logger({transports: transports});

我对你的代码进行了一些更改。我测试了它并且工作正常。它使用datepattern创建日志。

 console.log('Hello world');
var date1 = new Date();
var date = new Date();
var loggerName = "server.app.js";
var winston = require('winston');
var logger = new (winston.Logger)({
  transports: [
    new (winston.transports.DailyRotateFile)({ 
    name: 'file',
    datePattern: '.yyyy-MM-ddTHH-mm',
    filename: "log_file",
    maxFiles: 5,
    maxsize: 100000000



})
  ]
});



logger.info ('Logger Name: '+loggerName+' App starting...');
logger.info('Logger Name: ' + loggerName + 'Hello world');
logger.info('Logger Name: ' + loggerName + ' Accessing test/user');
logger.error('Logger Name: ' + loggerName + ' Cannot access test/user/user.png');

var express = require('express');
var app = express();
app.get('/', function (req, res) {
    res.send('Hello World!');
    var date2 = new Date();
    var diff = date2 - date1;
    logger.info('Start '+ date1+' Stop '+date2)
    logger.info('Logger Name: ' + loggerName + ' Response time of Node is ' + diff+' ms');
});

app.listen(3000, function () {
    logger.warn('Logger Name: ' + loggerName + ' Example app listening on port 3000!');
    logger.info('Logger Name: ' + loggerName + ' App ending...');
});