Commit 7ae3c13f by 马玮

Add log marks

parent 0e83aa86
......@@ -64,9 +64,7 @@ if (commandError.length > 0) {
output: process.stdout
});
DI.shutdownLogger(() => {
// do nothing
});
DI.shutdownLogger(() => {});
rl.write('正在释放资源...\n预计3秒后退出: ');
for (let i = 3; i > 0; i -= 1) {
......
......@@ -13,6 +13,9 @@ class DI {
this.params = params;
}
static init() {
}
static dir() {
console.log(App.root);
console.log(path.parse(process.mainModule.filename).dir);
......@@ -23,12 +26,23 @@ class DI {
console.log(process.cwd());
}
static getLogger() {
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 || {};
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{user}%] %m',
pattern: '%[[%d] [%p] [PID:%z] [%x{mark}%]] %m',
tokens: {
mark: (logEvent) => {
return this.LOG_MARK[category] || 'Master';
}
},
};
// 按文件大小回滚配置,待以后备用
// { type: 'file', filename: `${__basedir}/logs/dealer/carspider.log`, maxLogSize: 10485760, backups: 3, compress: false }
......
......@@ -16,7 +16,8 @@ class Base {
this.params = params;
this.loginTimes = 0;
this.maxLoginTimes = 3;
logger.addContext('user', 'mawei');
logger.addContext('user', 'accountID: 1');
this.logger = logger;
}
async execute() {
......
......@@ -22,6 +22,8 @@ axios.defaults.withCredentials = true;
class Autohome extends Base {
constructor(params) {
super(params);
this.logger.error('欢迎来到王者荣耀');
return;
}
async tryLogin() {
......
const Promise = require('bluebird');
const utils = require('./lib/utils');
const _ = require('./lib/utils/lodash');
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
process.env.NODE_CONFIG_DIR = __dirname + '/config';
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.');
return;
console.log(_.random(1, 5));
console.log(_.random(1, 5));
console.log(_.random(1, 5));
console.log(utils.md5('欢迎来到王者荣耀'));
return;
// const DI = require('./lib/DI');
// process.env.NODE_ENV = process.env.NODE_ENV || 'production';
// process.env.NODE_CONFIG_DIR = __dirname + '/config';
//
// const _ = require('lodash/core');
......
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