Commit ec631b16 by 马玮

Fix log mark

parent 7ae3c13f
......@@ -26,21 +26,15 @@ class DI {
console.log(process.cwd());
}
static setLogMark(category, context) {
this.LOG_MARK = this.LOG_MARK || {};
this.LOG_MARK[category] = context;
}
static getLogger(category = 'SPIDER') {
this.LOG_MARK = this.LOG_MARK || {};
static getLogger() {
const level = process.env.LOG_LEVEL || 'info';
const appenders = (process.env.LOG_TYPE ? process.env.LOG_TYPE.split(',') : null) || [ 'console', 'carspider' ];
const layout = {
type: 'pattern',
pattern: '%[[%d] [%p] [PID:%z] [%x{mark}%]] %m',
pattern: '%[[%d] [%p] [PID:%z|%x{mark}%]] %m',
tokens: {
mark: (logEvent) => {
return this.LOG_MARK[category] || 'Master';
return this.LOG_MARK || 'Main';
}
},
};
......@@ -63,7 +57,7 @@ class DI {
default: { appenders: appenders, level: level }
}
});
return log4js.getLogger('SPIDER');
return log4js.getLogger();
}
static shutdownLogger(cb) {
......
......@@ -9,7 +9,7 @@ captcha.decode = async (captchaType, imageFile) => {
console.info('captchaType:', captchaType);
console.info('imageFile:', imageFile);
const typeids = config.get('captcha.ruokuai.typeids');
typeid = typeids[captchaType];
const typeid = typeids[captchaType];
const captchaText = ruokuai.decode(typeid, imageFile);
return captchaText;
};
......
......@@ -58,7 +58,7 @@ class DealerHelper {
if (err) {
reject(err);
} else {
logger.info(`accountID: ${params.accountID}, cookie保存成功`);
logger.info(`cookie保存成功`);
resolve(true);
}
}
......@@ -120,7 +120,7 @@ class DealerHelper {
let sql;
// areaStrs.push('河南三门峡卢氏', '北京', '上海', '天津天津', '河南濮阳南乐', '河南郑州管城');
logger.info(`accountID: ${params.accountID}, 待匹配城市: ${JSON.stringify(areaStrs)}`);
logger.info(`待匹配城市: ${JSON.stringify(areaStrs)}`);
const getAreas = async (areaStrs, areaType) => {
// ${connection.escape(areaStrs.join())}
......@@ -178,7 +178,7 @@ class DealerHelper {
}
if (areaStrs.length > 0) {
logger.info(`accountID: ${params.accountID}, 以下城市匹配失败: ${JSON.stringify(areaStrs)}`);
logger.info(`以下城市匹配失败: ${JSON.stringify(areaStrs)}`);
}
resolve(mappedAreas);
......@@ -286,7 +286,7 @@ class DealerHelper {
}
clues = await this.mapClues(params).catch((e) => {
logger.error(`accountID: ${params.accountID}, 线索信息匹配失败: ${e}}`);
logger.error(`线索信息匹配失败: ${e}}`);
});
const connection = await DI.getConnection('MASTER_CRM');
......@@ -314,15 +314,15 @@ class DealerHelper {
area: clue.customerAreaID,
createTime: clue.createdAt
});
logger.info(`accountID: ${params.accountID}, 客户信息保存成功, customerID: ${clue.customerID}`);
logger.info(`客户信息保存成功, customerID: ${clue.customerID}`);
} catch (e) {
// 如果没有customerID则跳过这条线索
logger.error(`accountID: ${params.accountID}, 客户信息保存失败: ${e}}`);
logger.error(`客户信息保存失败: ${e}}`);
continue;
}
if (!clue.intentionID) {
logger.error(`accountID: ${params.accountID}, 线索车系匹配失败: ${clue.intentionDes}`);
logger.error(`线索车系匹配失败: ${clue.intentionDes}`);
continue;
}
......@@ -358,7 +358,7 @@ class DealerHelper {
if (results.affectedRows) {
// console.log(results.insertId, results);
logger.info(`accountID: ${params.accountID}, 线索信息保存成功, 总计: ${results.affectedRows}`);
logger.info(`线索信息保存成功, 总计: ${results.affectedRows}`);
if (redisSets.length > 0) {
await redis.multi(redisSets).exec();
......@@ -366,9 +366,9 @@ class DealerHelper {
try {
await this.saveSummary(params);
logger.info(`accountID: ${params.accountID}, 线索统计保存成功`);
logger.info(`线索统计保存成功`);
} catch (e) {
logger.info(`accountID: ${params.accountID}, 线索统计保存失败, ${e}`);
logger.info(`线索统计保存失败, ${e}`);
}
resolve(true);
......
......@@ -16,15 +16,14 @@ class Base {
this.params = params;
this.loginTimes = 0;
this.maxLoginTimes = 3;
logger.addContext('user', 'accountID: 1');
this.logger = logger;
DI.LOG_MARK = `AID:${this.params.accountID}`;
}
async execute() {
try {
await this[this.params.task]();
} catch (e) {
logger.fatal(`accountID: ${this.params.accountID}, 系统异常: ${e}`);
logger.fatal(`系统异常: ${e}`);
}
}
......@@ -36,27 +35,27 @@ class Base {
if (this.loginTimes < this.maxLoginTimes) {
this.loginTimes += 1;
logger.info(`accountID: ${this.params.accountID}, 开始尝试登录: ${this.loginTimes}次`);
logger.info(`开始尝试登录: ${this.loginTimes}次`);
try {
await this.tryLogin();
logger.info(`accountID: ${this.params.accountID}, 尝试登录成功: ${this.loginTimes}次`);
logger.info(`尝试登录成功: ${this.loginTimes}次`);
return 1;
} catch (e) {
logger.info(`accountID: ${this.params.accountID}, 尝试登录失败: ${this.loginTimes}次`);
logger.info(`尝试登录失败: ${this.loginTimes}次`);
}
logger.info(`accountID: ${this.params.accountID}, 开始表单登录: ${this.loginTimes}次`);
logger.info(`开始表单登录: ${this.loginTimes}次`);
try {
await this.login();
logger.info(`accountID: ${this.params.accountID}, 表单登录成功: ${this.loginTimes}次`);
logger.info(`表单登录成功: ${this.loginTimes}次`);
return 1;
} catch (e) {
logger.info(`accountID: ${this.params.accountID}, 表单登录失败: ${this.loginTimes}次`);
logger.info(`表单登录失败: ${this.loginTimes}次`);
return await this.retryLogin();
}
} else {
logger.error(`accountID: ${this.params.accountID}, 触发登录失败上限: ${this.loginTimes}次`);
logger.error(`触发登录失败上限: ${this.loginTimes}次`);
throw new errors.LoginError('登录失败');
}
}
......@@ -65,9 +64,9 @@ class Base {
async updateLatestFetch() {
try {
await DealerHelper.updateLatestFetch(this.params);
logger.info(`accountID: ${this.params.accountID}, 抓取时间更新成功`);
logger.info(`抓取时间更新成功`);
} catch (e) {
logger.error(`accountID: ${this.params.accountID}, 抓取时间更新失败, ${e}`);
logger.error(`抓取时间更新失败: ${e}`);
}
}
}
......
......@@ -22,8 +22,6 @@ axios.defaults.withCredentials = true;
class Autohome extends Base {
constructor(params) {
super(params);
this.logger.error('欢迎来到王者荣耀');
return;
}
async tryLogin() {
......@@ -37,7 +35,7 @@ class Autohome extends Base {
try {
oldCookies = await DealerHelper.getCookies(this.params);
} catch (e) {
logger.debug(`accountID: ${this.params.accountID}, 没有cookies: ${e}`);
logger.debug(`没有cookies: ${e}`);
}
if (oldCookies) {
......@@ -66,7 +64,7 @@ class Autohome extends Base {
const params = this.params;
params.cookies = newCookies;
logger.debug(`accountID: ${this.params.accountID}, cookies: ${JSON.stringify(newCookies)}`);
logger.debug(`cookies: ${JSON.stringify(newCookies)}`);
await DealerHelper.saveCookies(params);
return 1;
......@@ -82,7 +80,7 @@ class Autohome extends Base {
// 随机返回登录状态
// return [new errors.LoginError('登录失败'), _.random(0, 1)];
try {
logger.info(`accountID: ${this.params.accountID}, 开始表单登录`);
logger.info(`开始表单登录`);
const username = this.params.username;
const password = this.params.password;
......@@ -112,7 +110,7 @@ class Autohome extends Base {
try {
const captchaType = config.get(this.params.website)['captchaType'];
captchaText = await captcha.decodeBase64(captchaType, base64Text);
logger.info(`accountID: ${this.params.accountID}, captchaText: ${captchaText}`);
logger.info(`captchaText: ${captchaText}`);
} catch (e) {
logger.error(e);
return;
......@@ -148,7 +146,7 @@ class Autohome extends Base {
const params = this.params;
params.cookies = cookies;
logger.debug(`accountID: ${this.params.accountID}, cookies: ${JSON.stringify(cookies)}`);
logger.debug(`cookies: ${JSON.stringify(cookies)}`);
await DealerHelper.saveCookies(params);
}
......@@ -173,7 +171,7 @@ class Autohome extends Base {
} catch (e) {
// TODO: 需要考虑密码、验证码等因素
// TODO: 如果抓取线索过程中cookie失效可能也需要尝试重新登录
logger.error(`accountID: ${this.params.accountID}, 抓取线索登录失败: ${e}`);
logger.error(`抓取线索登录失败: ${e}`);
return;
}
......@@ -212,10 +210,10 @@ class Autohome extends Base {
params.productId = 0;
params.assignDealerId = 0;
logger.info(`accountID: ${this.params.accountID}, 线索抓取范围: ${params.startTime} - ${params.endtime}`);
logger.info(`线索抓取范围: ${params.startTime} - ${params.endtime}`);
const fetchByPage = async () => {
logger.info(`accountID: ${this.params.accountID}, 线索页面: ${params.pageIndex}`);
logger.info(`线索页面: ${params.pageIndex}`);
await axios.get('http://ics.Autohome.com.cn/Dms/Order/SearchOrderList', {
params: params,
headers:{
......@@ -263,7 +261,7 @@ class Autohome extends Base {
_.pick(clue, 'customerPhone', 'intentionDes', 'createDate'),
), true, true);
logger.info(`accountID: ${this.params.accountID}, hashBase: ${hashBase}`);
logger.info(`hashBase: ${hashBase}`);
let flag;
if (hashKeyExist) {
......@@ -275,7 +273,7 @@ class Autohome extends Base {
clues.push(clue);
}
logger.info(`accountID: ${this.params.accountID}, ${flag}线索: ${JSON.stringify(clue)}`);
logger.info(`${flag}线索: ${JSON.stringify(clue)}`);
logger.info('===============================================================================');
}
......@@ -288,13 +286,13 @@ class Autohome extends Base {
clueInputType: this.params.clueInputType,
});
} else {
logger.info(`accountID: ${this.params.accountID}, 时间段(${params.startTime} - ${params.endtime})没有线索`);
logger.info(`时间段(${params.startTime} - ${params.endtime})没有线索`);
}
// 判断是否抓取完全
if (customerIndex < rowCount) {
let randomSecs = _.random(1, 5);
logger.info(`accountID: ${this.params.accountID}, 随机等待${randomSecs}秒`);
logger.info(`随机等待${randomSecs}秒`);
await Promise.delay(randomSecs*1000).then(async function() {
params.pageIndex += 1;
await fetchByPage();
......@@ -304,9 +302,9 @@ class Autohome extends Base {
}
await fetchByPage();
await this.updateLatestFetch();
logger.info(`accountID: ${this.params.accountID}, 线索总数: ${rowCount}, 线索重复: ${rowDuplicate}, 新增线索: ${rowNew}`);
logger.info(`线索总数: ${rowCount}, 线索重复: ${rowDuplicate}, 新增线索: ${rowNew}`);
} catch (e) {
logger.error(`accountID: ${this.params.accountID}, ${e}`);
logger.error(`${e}`);
}
}
}
......
......@@ -35,7 +35,7 @@ class Easypass extends Base {
try {
oldCookies = await DealerHelper.getCookies(this.params);
} catch (e) {
logger.debug(`accountID: ${this.params.accountID}, 没有cookies: ${e}`);
logger.debug(`没有cookies: ${e}`);
}
if (oldCookies) {
......@@ -64,7 +64,7 @@ class Easypass extends Base {
const params = this.params;
params.cookies = newCookies;
logger.debug(`accountID: ${this.params.accountID}, cookies: ${JSON.stringify(newCookies)}`);
logger.debug(`cookies: ${JSON.stringify(newCookies)}`);
await DealerHelper.saveCookies(params);
......@@ -78,7 +78,7 @@ class Easypass extends Base {
// 随机返回登录状态
// return _.random(0, 1);
// return [new errors.LoginError('登录失败'), _.random(0, 1)];
logger.info(`accountID: ${this.params.accountID}, 开始表单登录`);
logger.info(`开始表单登录`);
const username = this.params.username;
const password = this.params.password;
......@@ -108,7 +108,7 @@ class Easypass extends Base {
try {
const captchaType = config.get(this.params.website)['captchaType'];
captchaText = await captcha.decodeBase64(captchaType, base64Text);
logger.info(`accountID: ${this.params.accountID}, captchaText: ${captchaText}`);
logger.info(`captchaText: ${captchaText}`);
} catch (e) {
logger.error(e);
return;
......@@ -144,7 +144,7 @@ class Easypass extends Base {
const params = this.params;
params.cookies = cookies;
logger.debug(`accountID: ${this.params.accountID}, cookies: ${JSON.stringify(cookies)}`);
logger.debug(`cookies: ${JSON.stringify(cookies)}`);
await DealerHelper.saveCookies(params);
}
......@@ -164,7 +164,7 @@ class Easypass extends Base {
} catch (e) {
// TODO:需要考虑密码、验证码等因素
// TODO:如果抓取线索过程中cookie失效可能也需要尝试重新登录
logger.error(`accountID: ${this.params.accountID}, 抓取线索登录失败: ${e}`);
logger.error(`抓取线索登录失败: ${e}`);
return;
}
......@@ -193,7 +193,7 @@ class Easypass extends Base {
}
const txtEndTime = moment().format('YYYY-MM-DD');
logger.info(`accountID: ${this.params.accountID}, 线索抓取范围: ${txtBeginTime} - ${txtEndTime}`);
logger.info(`线索抓取范围: ${txtBeginTime} - ${txtEndTime}`);
await
horseman
......@@ -250,7 +250,7 @@ class Easypass extends Base {
_.pick(clue, 'customerPhone', 'intentionDes', 'createDate'),
), true, true);
logger.info(`accountID: ${this.params.accountID}, hashBase: ${hashBase}`);
logger.info(`hashBase: ${hashBase}`);
rowCount += 1;
......@@ -264,7 +264,7 @@ class Easypass extends Base {
clues.push(clue);
}
logger.info(`accountID: ${this.params.accountID}, ${flag}线索: ${JSON.stringify(clue)}`);
logger.info(`${flag}线索: ${JSON.stringify(clue)}`);
logger.info('===============================================================================');
}
......@@ -276,7 +276,7 @@ class Easypass extends Base {
accountID: this.params.accountID,
});
} else {
logger.info(`accountID: ${this.params.accountID}, 时间段(${txtBeginTime} - ${txtEndTime})没有线索`);
logger.info(`时间段(${txtBeginTime} - ${txtEndTime})没有线索`);
}
// 判断是否最后一页有两种情况:1.只有一页时分页按钮不存在 2.末页按钮置灰
......@@ -295,7 +295,7 @@ class Easypass extends Base {
}
let randomSecs = _.random(1, 5);
logger.info(`accountID: ${this.params.accountID}, 随机等待${randomSecs}秒`);
logger.info(`随机等待${randomSecs}秒`);
await Promise.delay(randomSecs*1000).then(async function() {
await fetchByPage();
});
......@@ -304,9 +304,9 @@ class Easypass extends Base {
await fetchByPage();
await horseman.close();
await this.updateLatestFetch();
logger.info(`accountID: ${this.params.accountID}, 线索总数: ${rowCount}, 线索重复: ${rowDuplicate}, 新增线索: ${rowNew}`);
logger.info(`线索总数: ${rowCount}, 线索重复: ${rowDuplicate}, 新增线索: ${rowNew}`);
} catch (e) {
logger.error(`accountID: ${this.params.accountID}, ${e}`);
logger.error(`${e}`);
}
}
}
......
......@@ -3,31 +3,20 @@ const dealer = require('./dealer');
class Spider {
constructor(params) {
let self = this;
this.params = Object.assign({}, params);
this.spider = this.getSpider();
}
getSpider() {
let spider;
switch (this.params.website) {
case 'autoifeng': {
spider = new dealer.Autoifeng(this.params);
break;
}
case 'autohome': {
spider = new dealer.Autohome(this.params);
break;
}
case 'easypass': {
spider = new dealer.Easypass(this.params);
break;
}
const spiderClassMap = {
autoifeng: 'Autoifeng',
autohome: 'Autohome',
easypass: 'Easypass',
}
if (spider) {
return spider;
} else {
throw new Error('Spider not found');
try {
return new dealer[spiderClassMap[this.params.website]](this.params);
} catch (e) {
throw new Error(`Spider not found: ${e}`);
}
}
......
......@@ -8,17 +8,9 @@ global.__basedir = __dirname;
global.__libdir = __basedir + '/lib';
const DI = require('./lib/DI');
const logger = DI.getLogger();
DI.name = 'mawei';
DI.setLogMark('SPIDER', 'accountID:1');
console.log(DI.entrance);
logger.addContext('user', 'Main');
logger.trace('Entering cheese testing');
logger.debug('Got cheese.');
logger.info('Cheese is Gouda.');
logger.warn('Cheese is quite smelly.');
logger.error('Cheese is too ripe!');
logger.fatal('Cheese was breeding ground for listeria.');
DI.f = 'mawei';
DI.LOG_MARK = 'AID:1';
require('./test2');
return;
console.log(_.random(1, 5));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment