如何为 NodeJS 应用程序完成日志记录?

How can be logging done for a NodeJS application?

我想为我的 angularjs 应用程序实现带有警告信息和错误模式的日志记录。

使用 winston 模块 https://github.com/winstonjs/winston

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

winston.setLevels({ debug: 0, info: 1, silly: 2, warn: 3, error: 4, });

var fs = require('fs');
var dir = '../logfile';

if (!fs.existsSync(dir)) {
    fs.mkdirSync(dir);
}


var transport1 = new winston.transports.DailyRotateFile({
    name: 'info-log',
    // handleExceptions: true,
    // maxsize: 5 * 1024,
    // maxFiles: 3,
    filename: '../logfile/log',
    datePattern: 'SS_nodeserver-infolog-yyyy-MM-dd.',
    prepend: true,
    level: 'info'
});

var transport2 = new winston.transports.DailyRotateFile({
    name: 'error-log',
    // handleExceptions: true,
    // maxsize: 5 * 1024,
    // maxFiles: 3,
    filename: '../logfile/log',
    datePattern: 'SS_nodeserver-errorlog-yyyy-MM-dd.',
    prepend: true,
    level: 'error'
});

var loggerInstance = new winston.Logger({
    transports: [
        transport1,
        transport2
    ]
});
loggerInstance.logger.error("your error");
loggerInstance.logger.info("your info");