From 1502e4656050cab9d826e1695229868c5d543efd Mon Sep 17 00:00:00 2001 From: "zidong.pei" Date: Wed, 3 Feb 2021 14:27:11 +0800 Subject: [PATCH 1/2] use wildcard match log files --- lib/orders/error_log.js | 48 +++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/lib/orders/error_log.js b/lib/orders/error_log.js index d9c06c9..794275d 100644 --- a/lib/orders/error_log.js +++ b/lib/orders/error_log.js @@ -3,6 +3,8 @@ var fs = require('fs'); var helper = require('../utils'); var Parser = require('../error_parser'); +const { promisify } = require('util'); +var glob = promisify(require('glob')); var MAX_ERROR_COUNT = 20; // 一次最多抓获20个Error exports.logs = []; // 日志路径 @@ -11,8 +13,10 @@ var keyMap = new Map(); // 记录每个key实际对应的路径值 var map = new Map(); // 记录每个文件访问的位置 var parsers = new Map(); // 每个key都有一个parser -var getRealPath = function (filepath) { - return helper.resolveYYYYMMDDHH(filepath); +var getRealPath = async function (filepath) { + var files = await glob(filepath) + return files.map(path => helper.resolveYYYYMMDDHH(path)) + }; var readFile = function (key, filepath, callback) { @@ -62,20 +66,26 @@ var readFile = function (key, filepath, callback) { }); }; -var readLog = function (key, callback) { - var currentPath = getRealPath(key); - var current = keyMap.get(key); +var readLog = async function (key, callback) { + var currentPaths = await getRealPath(key); + var currents = keyMap.get(key); - if (currentPath !== current) { + if (currentPaths.toString() !== currents.toString()) { keyMap.set(key, currentPath); // replace real path - readFile(key, current, function (err) { - if (err) { - return callback(err); - } - readFile(key, currentPath, callback); - }); + currents.forEach(current => { + readFile(key, current, function (err) { + if (err) { + return callback(err); + } + currentPaths.forEach(path => { + readFile(key, path, callback); + }) + }); + }) } else { - readFile(key, currentPath, callback); + currentPaths.forEach(path => { + readFile(key, path, callback); + }) } }; @@ -94,7 +104,7 @@ var readLogs = function (callback) { } }; -exports.init = function (config) { +exports.init = async function (config) { if (config.error_log) { exports.logs = config.error_log; var logs = config.error_log; @@ -102,10 +112,12 @@ exports.init = function (config) { for (var i = 0; i < logs.length; i++) { var key = logs[i]; parsers.set(key, new Parser(MAX_ERROR_COUNT)); - var realPath = getRealPath(key); - if (fs.existsSync(realPath)) { - map.set(realPath, fs.statSync(realPath).size); - } + var realPath = await getRealPath(key); + realPath.forEach(path => { + if (fs.existsSync(path)) { + map.set(path, fs.statSync(path).size); + } + }) keyMap.set(key, realPath); } } From 69970beb60813a99ef2127a7051e89064cab8b84 Mon Sep 17 00:00:00 2001 From: "zidong.pei" Date: Wed, 3 Feb 2021 16:10:15 +0800 Subject: [PATCH 2/2] Complete ES6 verification Complete ES6 verification Complete ES6 verification fix inexist error logs test install alinode shell Add executable permission Add executable permission Add executable permission back Add executable permission back Add executable permission change bash to sh change bash to sh Use wget download tnvm Use wget download tnvm source .bashrc Rollback system test echo TRAVIS_NODE_VERSION Install alinode according to the node version Install alinode according to the node version Install alinode according to the node version which node Add wildcard match log test --- .gitignore | 1 + .travis.yml | 6 ++- install_alinode.sh | 27 ++++++++++++ lib/orders/error_log.js | 77 +++++++++++++++++++++-------------- test/orders/error_log.test.js | 15 +++++++ 5 files changed, 94 insertions(+), 32 deletions(-) create mode 100755 install_alinode.sh diff --git a/.gitignore b/.gitignore index fdd4145..f629ba6 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ config_*.json core.* coredump_* .nyc_output/ +.vscode diff --git a/.travis.yml b/.travis.yml index f7fd2ad..2a582f7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,4 +4,8 @@ node_js: - "6" - "8" - "10" -script: npm run ci +script: + - npm run ci + +before_install: + - ./install_alinode.sh diff --git a/install_alinode.sh b/install_alinode.sh new file mode 100755 index 0000000..188d841 --- /dev/null +++ b/install_alinode.sh @@ -0,0 +1,27 @@ +wget --no-check-certificate -O- https://raw.githubusercontent.com/aliyun-node/tnvm/master/install.sh | bash +source $HOME/.bash_profile +source $HOME/.bashrc +echo TRAVIS_NODE_VERSION = $TRAVIS_NODE_VERSION + +if [ $TRAVIS_NODE_VERSION -eq '6' ];then + tnvm install alinode-v2.9.0 + tnvm use alinode-v2.9.0 +elif [ $TRAVIS_NODE_VERSION -eq '8' ];then + tnvm install alinode-v3.16.0 + tnvm use alinode-v3.16.0 +elif [ $TRAVIS_NODE_VERSION -eq '10' ];then + tnvm install alinode-v4.13.2 + tnvm use alinode-v4.13.2 +else + tnvm install alinode-v6.4.4 + tnvm use alinode-v6.4.4 +fi + +tnvm current + +echo 'which node' +which node + +source $HOME/.bash_profile +source $HOME/.bashrc + diff --git a/lib/orders/error_log.js b/lib/orders/error_log.js index 794275d..ab251d0 100644 --- a/lib/orders/error_log.js +++ b/lib/orders/error_log.js @@ -3,8 +3,7 @@ var fs = require('fs'); var helper = require('../utils'); var Parser = require('../error_parser'); -const { promisify } = require('util'); -var glob = promisify(require('glob')); +var glob = require('glob'); var MAX_ERROR_COUNT = 20; // 一次最多抓获20个Error exports.logs = []; // 日志路径 @@ -13,12 +12,14 @@ var keyMap = new Map(); // 记录每个key实际对应的路径值 var map = new Map(); // 记录每个文件访问的位置 var parsers = new Map(); // 每个key都有一个parser -var getRealPath = async function (filepath) { - var files = await glob(filepath) - return files.map(path => helper.resolveYYYYMMDDHH(path)) - +var getRealPath = function (filepath,callback) { + glob(filepath,callback); }; +// var a = function (err,file){ +// return file.map(path => helper.resolveYYYYMMDDHH(path)) +// } + var readFile = function (key, filepath, callback) { fs.stat(filepath, function (err, stats) { if (err) { @@ -66,27 +67,37 @@ var readFile = function (key, filepath, callback) { }); }; -var readLog = async function (key, callback) { - var currentPaths = await getRealPath(key); - var currents = keyMap.get(key); - - if (currentPaths.toString() !== currents.toString()) { - keyMap.set(key, currentPath); // replace real path - currents.forEach(current => { - readFile(key, current, function (err) { - if (err) { - return callback(err); - } +var readLog = function (key, callback) { + getRealPath(key,function(_err,files){ + var currentPaths = files.map(path => helper.resolveYYYYMMDDHH(path)); + var currents = keyMap.get(key); + + if (currentPaths.toString() !== currents.toString()) { + keyMap.set(key, currentPaths); // replace real path + currents.forEach(current => { + readFile(key, current, function (err) { + if (err) { + return callback(err); + } + if( currentPaths.length !== 0) { + currentPaths.forEach(path => { + readFile(key, path, callback); + }); + } else { + readFile(key, '', callback); + } + }); + }); + } else { + if( currentPaths.length !== 0) { currentPaths.forEach(path => { readFile(key, path, callback); - }) - }); - }) - } else { - currentPaths.forEach(path => { - readFile(key, path, callback); - }) - } + }); + }else{ + readFile(key, '', callback); + } + } + }); }; var readLogs = function (callback) { @@ -104,21 +115,25 @@ var readLogs = function (callback) { } }; -exports.init = async function (config) { +exports.init = function (config) { if (config.error_log) { exports.logs = config.error_log; var logs = config.error_log; - for (var i = 0; i < logs.length; i++) { - var key = logs[i]; - parsers.set(key, new Parser(MAX_ERROR_COUNT)); - var realPath = await getRealPath(key); + var key; + var initCallBack = function(_err,files){ + var realPath = files.map(path => helper.resolveYYYYMMDDHH(path)); realPath.forEach(path => { if (fs.existsSync(path)) { map.set(path, fs.statSync(path).size); } - }) + }); keyMap.set(key, realPath); + }; + for (var i = 0; i < logs.length; i++) { + key = logs[i]; + parsers.set(key, new Parser(MAX_ERROR_COUNT)); + getRealPath(key,initCallBack); } } }; diff --git a/test/orders/error_log.test.js b/test/orders/error_log.test.js index ae150b9..185f2e4 100644 --- a/test/orders/error_log.test.js +++ b/test/orders/error_log.test.js @@ -95,6 +95,21 @@ describe('/lib/orders/error_log.js', function () { }); }); + describe('wildcard match log', function () { + before(function () { + errorLog.init({ + error_log: [path.join(__dirname, '../logs', '*.log')] + }); + }); + + it('should ok', function (done) { + errorLog.run(function (err) { + expect(err).to.not.be.ok(); + done(); + }); + }); + }); + describe('when path is folder error logs', function () { before(function () { errorLog.init({