var log = require('zhuge-log');
var path = require('path');
log.setConfig({
fileName: 'log-%DATE%.log',// <required> 日志路径
pattern: 'YYYY-MM-DD',// <required> 日志格式
logPath: '/data/logs/project/',//<required>日志路径
emailCachePath: path.join(__dirname,'../../checklog/'),// 防止同一进程重发相同问题的邮件缓存路径,最好放到项目中,只会有一个文件txt
project: 'project',// 当前项目名称
env: 'test', // 修改当前环境 'develop' || 'test' || 'release'
host: '',
port: 000,
secure: true,// true for 465, false for other ports
user: 'haha@qq.com', // generated ethereal user
pass: '123123213', // generated ethereal password
from: 'asdfa@qq.com', // sender address
to: 'fasdsad6@163.com', // list of receivers 多个邮箱","分开
canSend: false,//是否可以发送邮件,只对ejs渲染出错是否发送邮件起作用,自定义邮件不起作用(false:不发送;true:发送)
});
// 添加邮件功能
module.exports = {
logger: log.logger(),
outPutLog: log.outPutLog,
email: log.email
};
var zhugelog = require('./logger');
var outPutLog = zhugelog.outPutLog;
req.post(url, urlPara, function (err, res, body) {
var endTime = new Date().getTime();
var durTime = endTime - strTime;
// console.log(url);
// console.log(urlPara);
// 获取接口的响应时间 毫秒级
// console.log(durTime,'接口响应时间****************************');
try {
if (err){
// ==================catch error start==============================
outPutLog.requestError({
durTime: durTime,
method: 'post',
req: request,
url: url,
urlPara: urlPara,
error: err,
body: body
});
// ==================catch error end==============================
callback(err,requestDeal(err,{}));
}
else {
if (res && res.headers && res.headers['content-encoding'] && res.headers['content-encoding'] === 'gzip') {
zlib.unzip(body, function (err, buffer) {
try {
body = JSON.parse(buffer.toString());
if (body.code!==200 && body.success !== true && body.errcode !== 0){
// ==================catch error start==============================
outPutLog.requestCodeError({
durTime: durTime,
method: 'post',
req: request,
url: url,
urlPara: urlPara,
error: err,
body: body
});
// ==================catch error end==============================
}
callback(null, requestDeal(err,body));
} catch (e) {
// ==================catch error start==============================
outPutLog.requestCodeError({
durTime: durTime,
method: 'post',
req: request,
url: url,
urlPara: urlPara,
error: e,
body: body
});
// ==================catch error end================================
callback(e,requestDeal(e,{}));
}
});
} else {
if (Buffer.isBuffer(body)) {
try {
// JSON.parse 可能报错
body = JSON.parse(body.toString());
if (body.code!==200 && body.success !== true && body.errcode !== 0){
// ==================catch error start==============================
outPutLog.requestCodeError({
durTime: durTime,
method: 'post',
req: request,
url: url,
urlPara: urlPara,
error: err,
body: body
});
// ==================catch error end==============================
}
callback(null, requestDeal(err,body));
} catch (e) {
// ==================catch error start==============================
outPutLog.requestCodeError({
durTime: durTime,
method: 'post',
req: request,
url: url,
urlPara: urlPara,
error: e,
body: body
});
// ==================catch error end================================
callback(e,requestDeal(e,{}));
}
} else {
// console.log(body,'body');
if (body.code!==200 && body.success !== true && body.errcode !== 0){
// ==================catch error start==============================
outPutLog.requestCodeError({
durTime: durTime,
method: 'post',
req: request,
url: url,
urlPara: urlPara,
error: err,
body: body
});
// ==================catch error end==============================
}
callback(null, requestDeal(err,body));
}
}
}
} catch (e) {
// ==================catch error start==============================
outPutLog.requestCatchError({
durTime: durTime,
method: 'post',
req: request,
url: url,
urlPara: urlPara,
error: e,
body: body
});
// ==================catch error end================================
callback(e,requestDeal(e,{}));
}
});