码迷,mamicode.com
首页 > Web开发 > 详细

nodejs日志管理log4js

时间:2015-08-25 14:12:37      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

常用的2种配置:

1.按文件大小分片,备份若干数量的文件

var log4js = require(‘log4js‘);

log4js.configure({
    "appenders" : [
        {"type" : "console"},
        {
            "type" : "file",
            "filename" : "logs/app.log",
            "maxLogSize" : 204800,
            "backups" : 10,
            "category" : "app"
        }
    ],
    "replaceConsole": true,
    "levels" : {
        "app" : "ALL"
    }
});

var logger = log4js.getLogger(‘app‘);

app.use(log4js.connectLogger(logger, {format: ‘:remote-addr :method :url :status :response-time ms‘})

2.按日期分片,此分片方式不可以进行大小和备份数量的限制

var log4js = require(‘log4js‘);

log4js.configure({
    appenders : [
        {type : ‘console‘},
        {
            type : ‘dateFile‘,
            filename : ‘logs/app‘,
            pattern : ‘-yyyy-MM-dd.log‘,
            alwaysIncludePattern : true,
            category : ‘app‘
        }
    ],
    replaceConsole: true
});
var logger = log4js.getLogger(‘app‘);

app.use(log4js.connectLogger(logger, {level: ‘ALL‘, format: ‘:remote-addr :method :url :status :response-time ms‘}));

 

多类型多应用文件分片配置:

log4js.configure({
    "appenders" : [
        {"type" : "console"},
        {
            "type" : "file",
            "filename" : "logs/app1.log",
            "maxLogSize" : 204800,
            "backups" : 10,
            "category" : "app1"
        },
        {
            "type" : "file",
            "filename" : "logs/app2.log",
            "maxLogSize" : 204800,
            "backups" : 10,
            "category" : "app2"
        },
        {
            "type" : "file",
            "filename" : "logs/app3.log",
            "maxLogSize" : 204800,
            "backups" : 10,
            "category" : "app3"
        }
    ],
    "replaceConsole": true,
    "levels" : {
        "app1" : "ALL",  //等级可以设置ALL,AUTO,INFO,WARN,ERROR
        "app2" : "ALL",
        "app3" : "ALL"
    }
});

 

模块化:

1.定义配置文件log4js.json

{
    "appenders" : [
        {"type" : "console"},
        {
            "type" : "dateFile",
            "filename" : "logs/app",
            "pattern" : "-yyyy-MM-dd.log",
            "alwaysIncludePattern" : true,
            "category" : "app"
        }
    ],
    "replaceConsole": true,
    "levels" : {
        "app" : "ALL"
    }
}

2.核心模ms.js

/*
 * log4js
 */
var log4js = require("log4js");
exports.log4js_config = require("../log4js.json");
log4js.configure(this.log4js_config);
exports.logger = log4js.getLogger("app");

3.使用方法

var ms = require("ms");

ms.logger.info("INFO TEST");
ms.logger.warn("WARN TEST");
ms.logger.error("ERROR TEST");

 

nodejs日志管理log4js

标签:

原文地址:http://www.cnblogs.com/kevalin/p/4757027.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!