标签:
常用的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");
标签:
原文地址:http://www.cnblogs.com/kevalin/p/4757027.html