代码拉取完成,页面将自动刷新
var log4js = require('log4js');
var fs = require("fs");
var path = require("path");
var helper = {};
// 加载配置文件
var objConfig = JSON.parse(fs.readFileSync("log4js.json", "utf8"));
// 检查配置文件所需的目录是否存在,不存在时创建
if(objConfig.appenders){
var baseDir = 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 logDefault = log4js.getLogger('logDefault')
helper.writeDebug = function(msg){
if(msg == null)
msg = "";
logDebug.debug(msg);
};
helper.writeInfo = function(msg){
if(msg == null)
msg = "";
logInfo.info(msg);
};
helper.writeWarn = function(msg){
if(msg == null)
msg = "";
logWarn.warn(msg);
};
helper.writeErr = function(msg, exp){
if(msg == null)
msg = "";
if(exp != null)
msg += "\r\n" + exp;
logErr.error(msg);
};
// 判断日志目录是否存在,不存在时创建日志目录
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] == "/";
}
}
// 配合express用的方法
exports.use = function(app) {
//页面请求日志, level用auto时,默认级别是WARN
app.use(log4js.connectLogger(logDefault,
{level:'DEBUG', format:':method :status :url'}));
app.use(log4js.connectLogger(logWarn,
{level:'WARN', format:':method :status :url'}));
app.use(log4js.connectLogger(logErr,
{level:'error', format:':method :status :url'}));
}
exports.helper = helper;
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。