Browse Source

add common log/info/warn/error functions

master
filesite 8 months ago
parent
commit
7ddcf89e84
  1. 61
      common.mjs
  2. 6
      conf/config.json
  3. 16
      heroUnion.mjs
  4. 2
      router_api.mjs
  5. 40
      test/common.test.mjs
  6. 9
      test/heroUnion.test.mjs

61
common.mjs

@ -22,6 +22,20 @@ class Common { @@ -22,6 +22,20 @@ class Common {
return Math.floor(Date.now() / 1000);
}
getLocalTimeString(locales, timezone) {
if (typeof(locales) == 'undefined' || !locales) {
locales = 'zh-Hans-CN';
}
if (typeof(timezone) == 'undefined' || !timezone) {
timezone = 'Asia/Shanghai';
}
let date = new Date();
let option = {"timeZone": timezone};
return date.toLocaleString(locales, option);
}
sortDict(obj) { //dict按key排序
return Object.keys(obj).sort().reduce(function (result, key) {
result[key] = obj[key];
@ -106,6 +120,53 @@ class Common { @@ -106,6 +120,53 @@ class Common {
return /^\w{6,32}$/i.test(bot_name);
}
getLogArguments() {
let args = [];
let localTime = this.getLocalTimeString('zh-Hans-CN', 'Asia/Shanghai');
if (arguments[0]) {
let logFormat = `[%s] ${arguments[0]}`;
args.push(logFormat);
}
if (arguments) {
for (const index in arguments) {
if (index > 1) {
args.push(arguments[index]);
}else if (index == 1) {
args.push(localTime);
args.push(arguments[index]);
}
}
}
return args;
}
log() {
let args = this.getLogArguments.apply(this, arguments);
console.log.apply(this, args);
return args;
}
info() {
let args = this.getLogArguments.apply(this, arguments);
console.info.apply(this, args);
return args;
}
warn() {
let args = this.getLogArguments.apply(this, arguments);
console.warn.apply(this, args);
return args;
}
error() {
let args = this.getLogArguments.apply(this, arguments);
console.error.apply(this, args);
return args;
}
}
let commonFuns = new Common();

6
conf/config.json

@ -5,11 +5,11 @@ @@ -5,11 +5,11 @@
"task_data_max_size": 1024,
"task_cache_time": 86400,
"heroHeartTimeout": 20,
"heroHeartTimeout": 600,
"notify_timeout": 8,
"reloadConfigFrequence": 10,
"heroHeartCheckFrequence": 10,
"reloadConfigFrequence": 60,
"heroHeartCheckFrequence": 60,
"tokens": {
"demo": "hello#world!"

16
heroUnion.mjs

@ -271,12 +271,12 @@ class HeroUnion { @@ -271,12 +271,12 @@ class HeroUnion {
if (response.status == 200) {
notified = true;
}else {
console.error('[FAILED] Notify to %s failed, response status: %s, status text: %s, result: %s',
common.error('[FAILED] Notify to %s failed, response status: %s, status text: %s, result: %s',
notify_url, response.status, response.statusText, response.data);
}
}
}catch(err) {
console.error('[ERROR] Notify to %s failed: %s', notify_url, err);
common.error('[ERROR] Notify to %s failed: %s', notify_url, err);
}
return notified;
@ -301,14 +301,14 @@ class HeroUnion { @@ -301,14 +301,14 @@ class HeroUnion {
if (cachedBot) { //如果是已经存在的爬虫
if (cachedBot.status != bot.status) {
console.log('Hero %s status change from %s to %s', cachedBot.name, cachedBot.status, bot.status);
common.log('Hero %s status change from %s to %s', cachedBot.name, cachedBot.status, bot.status);
this.heroStatus[cachedBot.status] --;
this.heroStatus[bot.status] ++;
}
this.heros[cachedBotIndex] = bot; //数据更新
console.log('Hero %s is %s at %s', bot.name, bot.status, bot.timestamp);
common.log('Hero %s is %s at %s', bot.name, bot.status, bot.timestamp);
}else {
this.heros.push(bot); //添加新爬虫
@ -319,7 +319,7 @@ class HeroUnion { @@ -319,7 +319,7 @@ class HeroUnion {
this.heroStatus.busy ++;
}
console.log('Hero %s is onboard at %s', bot.name, bot.timestamp);
common.log('Hero %s is onboard at %s', bot.name, bot.timestamp);
}
}
@ -338,7 +338,7 @@ class HeroUnion { @@ -338,7 +338,7 @@ class HeroUnion {
_self.heros[index].status = 'offline';
_self.heroStatus.offline ++;
console.log('Hero %s is offline, last heart beat at %s', item.name, item.timestamp);
common.log('Hero %s is offline, last heart beat at %s', item.name, item.timestamp);
}
});
}, {
@ -346,7 +346,7 @@ class HeroUnion { @@ -346,7 +346,7 @@ class HeroUnion {
});
cronjob.start();
console.log('Cronjob of hero heart check started.');
common.log('Cronjob of hero heart check started.');
}
//自动重新加载配置文件
@ -363,7 +363,7 @@ class HeroUnion { @@ -363,7 +363,7 @@ class HeroUnion {
});
cronjob.start();
console.log('Cronjob of config auto reload started.');
common.log('Cronjob of config auto reload started.');
}
//获取联盟状态

2
router_api.mjs

@ -16,6 +16,8 @@ const router = express.Router(); @@ -16,6 +16,8 @@ const router = express.Router();
router.get('/', async (req, res) => {
const apiList = {
"/api/": "查看所有API",
"/api/onboard/": "爬虫状态上报到联盟",
"/api/stats/": "查看联盟状态",
};

40
test/common.test.mjs

@ -41,3 +41,43 @@ test('Common function getConfigFromJsonFile test', async (t) => { @@ -41,3 +41,43 @@ test('Common function getConfigFromJsonFile test', async (t) => {
const expectName = 'Hero Union';
assert.strictEqual(config.name, expectName);
});
test('Common function getLocalTimeString test', async (t) => {
let timeString = common.getLocalTimeString('zh-CN', 'Asia/Shanghai');
console.log('北京时间:%s', timeString);
assert.ok(timeString);
timeString = common.getLocalTimeString('zh-HK', 'UTC');
console.log('香港UTC时间:%s', timeString);
assert.ok(timeString);
});
test('Common function log/info/warn/error test', async (t) => {
let string = '测试log输出';
let args = common.log('console.log替换测试:%s', string);
assert.ok(args);
assert.equal(/^\[%s\] /i.test(args[0]), true);
assert.equal(args.length, 3);
assert.equal(args[args.length - 1], string);
args = common.info('console.info替换测试:%s', string);
assert.ok(args);
assert.equal(/^\[%s\] /i.test(args[0]), true);
assert.equal(args.length, 3);
assert.equal(args[args.length - 1], string);
args = common.warn('console.warn替换测试:%s', string);
assert.ok(args);
assert.equal(/^\[%s\] /i.test(args[0]), true);
assert.equal(args.length, 3);
assert.equal(args[args.length - 1], string);
args = common.error('console.error替换测试:%s', string);
assert.ok(args);
assert.equal(/^\[%s\] /i.test(args[0]), true);
assert.equal(args.length, 3);
assert.equal(args[args.length - 1], string);
console.log("插入日期后的参数:\n%s", args);
});

9
test/heroUnion.test.mjs

@ -14,6 +14,15 @@ const axiosConfig = { @@ -14,6 +14,15 @@ const axiosConfig = {
proxy: false
};
test('HeroUnion api list test', async (t) => {
let api = 'http://127.0.0.1:8080/api/';
const response = await axios.get(api, axiosConfig);
console.log(response.data);
assert.equal(response.status, 200);
});
test('Hero onboard test', async (t) => {
let params = {
name: 'test_hero',

Loading…
Cancel
Save