Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ config_*.json
core.*
coredump_*
.nyc_output/
.vscode
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ node_js:
- "6"
- "8"
- "10"
script: npm run ci
script:
- npm run ci

before_install:
- ./install_alinode.sh
27 changes: 27 additions & 0 deletions install_alinode.sh
Original file line number Diff line number Diff line change
@@ -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

69 changes: 48 additions & 21 deletions lib/orders/error_log.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var fs = require('fs');
var helper = require('../utils');
var Parser = require('../error_parser');
var glob = require('glob');

var MAX_ERROR_COUNT = 20; // 一次最多抓获20个Error
exports.logs = []; // 日志路径
Expand All @@ -11,10 +12,14 @@ 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 = 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) {
Expand Down Expand Up @@ -63,20 +68,36 @@ var readFile = function (key, filepath, callback) {
};

var readLog = function (key, callback) {
var currentPath = getRealPath(key);
var current = keyMap.get(key);

if (currentPath !== current) {
keyMap.set(key, currentPath); // replace real path
readFile(key, current, function (err) {
if (err) {
return callback(err);
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{
readFile(key, '', callback);
}
readFile(key, currentPath, callback);
});
} else {
readFile(key, currentPath, callback);
}
}
});
};

var readLogs = function (callback) {
Expand All @@ -99,14 +120,20 @@ exports.init = function (config) {
exports.logs = config.error_log;
var logs = config.error_log;

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++) {
var key = logs[i];
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);
}
keyMap.set(key, realPath);
getRealPath(key,initCallBack);
}
}
};
Expand Down
15 changes: 15 additions & 0 deletions test/orders/error_log.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down