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

时间:2017-10-11 11:36:31

标签: angularjs node.js logging error-logging

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

1 个答案:

答案 0 :(得分:0)

使用 winston 模块 https://github.com/RSATom/QmlVlcDemo

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");