node项目中引入log4日志的部署
node项目中引入log4日志的部署,log4js.json 日志配置文件及含义
{ "customBaseDir" :"logs/", //日志生成目录,相对于项目根目录 "customDefaultAtt" :{ //日志记录默认参数配置 "type": "dateFile", // 日志文件类型,可以使用日期作为文件名的占位符 "absolute":true , //日志文件路径为绝对路径 "alwaysIncludePattern": true //文件名始终使用日期占位符 }, "appenders": [ {"type": "console", "category": "console"}, //配合express框架输出web url请求日志 //具体的日志记录器 { "pattern": "debug/yyyyMMddhh.txt", //日志文件占位符 "category": "logDebug" //日志记录器名,即日至的打印类型 }, {"pattern": "info/yyyyMMddhh.txt", "category": "logInfo"}, {"pattern": "warn/yyyyMMddhh.txt", "category": "logWarn"}, {"pattern": "err/yyyyMMddhh.txt", "category": "logErr"} ], "replaceConsole": true, "levels":{ "logDebug": "DEBUG", "logInfo": "DEBUG", "logWarn": "DEBUG", "logErr": "DEBUG"} //设置日志记录打印级别,低于DEBUG级别的日志不予输出 }
log4js日志的命令生成
var helper = {}; exports.helper = helper; var log4js = require('log4js'); var fs = require("fs"); var path = require("path"); // 加载配置文件 var objConfig = JSON.parse(fs.readFileSync("log4js.json", "utf8")); // 检查配置文件所需的目录是否存在,不存在时创建 if(objConfig.appenders){ var baseDir = path.join(__dirname,objConfig["customBaseDir"]); var defaultAtt = objConfig["customDefaultAtt"]; for(var i= 0, j=objConfig.appenders.length; i<j; i++){ var item = objConfig.appenders[i]; if(item["type"] == "console") continue; if(defaultAtt != null){ for(var att in defaultAtt){ if(item[att] == null) item[att] = defaultAtt[att]; } } if(baseDir != null){ if(item["filename"] == null) item["filename"] = baseDir; //日志文件路径 else item["filename"] = baseDir + item["filename"]; } var fileName = item["filename"]; if(fileName == null) continue; var pattern = item["pattern"]; if(pattern != null){ fileName += pattern; } var category = item["category"]; if(!isAbsoluteDir(fileName))//path.isAbsolute(fileName)) throw new Error("配置节" + category + "的路径不是绝对路径:" + fileName); var dir = path.dirname(fileName); checkAndCreateDir(dir); } } // 目录创建完毕,才加载配置,不然会出异常 log4js.configure(objConfig); var logDebug = log4js.getLogger('logDebug'); var logInfo = log4js.getLogger('logInfo'); var logWarn = log4js.getLogger('logWarn'); var logErr = log4js.getLogger('logErr'); var logConsole = log4js.getLogger('console'); helper.debug = function(msg){ if(msg == null) msg = ""; logDebug.debug(msg); logConsole.log(msg); }; helper.info = function(msg){ if(msg == null) msg = ""; logInfo.info(msg); logConsole.info(msg); }; helper.warn = function(msg){ if(msg == null) msg = ""; logWarn.warn(msg); logConsole.warn(msg); }; helper.error = function(msg, exp){ if(msg == null) msg = ""; if(exp != null) msg += "\r\n" + exp; logErr.error(msg); logConsole.error(msg); }; // 配合express用的方法 exports.use = function(app) { //页面请求日志, level用auto时,默认级别是WARN app.use(log4js.connectLogger(logInfo, {level:'debug', format:':method :url'})); } // 判断日志目录是否存在,不存在时创建日志目录 function checkAndCreateDir(dir){ if(!fs.existsSync(dir)){ fs.mkdirSync(dir); } } // 指定的字符串是否绝对路径 function isAbsoluteDir(path){ if(path == null) return false; var len = path.length; var isWindows = process.platform === 'win32'; if(isWindows){ if(len <= 1) return false; return path[1] == ":"; }else{ if(len <= 0) return false; return path[0] == "/"; } }