From 7ddcf89e8468bb60cb716526771659c5dbc02886 Mon Sep 17 00:00:00 2001 From: filesite Date: Tue, 9 Apr 2024 10:14:50 +0800 Subject: [PATCH] add common log/info/warn/error functions --- common.mjs | 61 +++++++++++++++++++++++++++++++++++++++++ conf/config.json | 6 ++-- heroUnion.mjs | 16 +++++------ router_api.mjs | 2 ++ test/common.test.mjs | 40 +++++++++++++++++++++++++++ test/heroUnion.test.mjs | 9 ++++++ 6 files changed, 123 insertions(+), 11 deletions(-) diff --git a/common.mjs b/common.mjs index 64d40c3..1d1e6d1 100644 --- a/common.mjs +++ b/common.mjs @@ -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 { 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(); diff --git a/conf/config.json b/conf/config.json index c05126a..cf44f34 100644 --- a/conf/config.json +++ b/conf/config.json @@ -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!" diff --git a/heroUnion.mjs b/heroUnion.mjs index f6f7bf4..30d8b5b 100644 --- a/heroUnion.mjs +++ b/heroUnion.mjs @@ -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 { 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 { 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 { _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 { }); cronjob.start(); - console.log('Cronjob of hero heart check started.'); + common.log('Cronjob of hero heart check started.'); } //自动重新加载配置文件 @@ -363,7 +363,7 @@ class HeroUnion { }); cronjob.start(); - console.log('Cronjob of config auto reload started.'); + common.log('Cronjob of config auto reload started.'); } //获取联盟状态 diff --git a/router_api.mjs b/router_api.mjs index 0c10663..490c116 100644 --- a/router_api.mjs +++ b/router_api.mjs @@ -16,6 +16,8 @@ const router = express.Router(); router.get('/', async (req, res) => { const apiList = { "/api/": "查看所有API", + + "/api/onboard/": "爬虫状态上报到联盟", "/api/stats/": "查看联盟状态", }; diff --git a/test/common.test.mjs b/test/common.test.mjs index 353acca..03e995b 100644 --- a/test/common.test.mjs +++ b/test/common.test.mjs @@ -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); +}); diff --git a/test/heroUnion.test.mjs b/test/heroUnion.test.mjs index e20a79b..1d31ddf 100644 --- a/test/heroUnion.test.mjs +++ b/test/heroUnion.test.mjs @@ -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',