Node.js log4js日志记录

这次需要给之前弄的文件服务器添加日志记录,一般每天产生的日志会特别多所以安装日期来划分是最好的,这里我用了express框架,为了适应express框架这里在log.js文件中写了use方法。

//日志记录
var log4js = require('log4js');
log4js.configure({
    appenders: [
        {
            type: 'console',
            category: "console"
        },
        {
            type : 'dateFile',
            filename : 'logs/',
            pattern : '-yyyy-MM-dd.log',
            alwaysIncludePattern : true,
            category : 'record'
        }
    ],
    replaceConsole: true,
    levels:{
        dateFileLog: 'all',
        console: 'all'
    }
});

var dateFileLog = log4js.getLogger('record');
var consoleLog = log4js.getLogger('console');
exports.logger = dateFileLog;


exports.use = function(app) {
    app.use(log4js.connectLogger(consoleLog, {level:'INFO', format:':method :url'}));
}


最后在app.js中添加以下代码就可以了

var log4js = require("./log.js");
log4js.use(app);
var logger = log4js.logger;
logger.debug("currentTime=%s",new Date());


另外添上感觉整理的比较好的几篇文章,方便查找

nodejs日志管理log4js常用配置

http://www.cnblogs.com/kevalin/p/4757027.html
http://www.cnblogs.com/Joans/p/4092293.html

Node.js中的日志管理模块使用与封装

http://blog.csdn.net/youbl/article/details/32708609



原文地址:https://www.cnblogs.com/chenjianxiang/p/6183277.html