Skip to content

Commit f1040c8

Browse files
author
zidong.pei
committed
use wildcard match log files
1 parent 4796468 commit f1040c8

File tree

5 files changed

+96
-22
lines changed

5 files changed

+96
-22
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ config_*.json
88
core.*
99
coredump_*
1010
.nyc_output/
11+
.vscode

.travis.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ node_js:
44
- "6"
55
- "8"
66
- "10"
7-
script: npm run ci
7+
script:
8+
- npm run ci
9+
10+
before_install:
11+
- ./install_alinode.sh

install_alinode.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
wget --no-check-certificate -O- https://raw.githubusercontent.com/aliyun-node/tnvm/master/install.sh | bash
2+
source $HOME/.bash_profile
3+
source $HOME/.bashrc
4+
echo TRAVIS_NODE_VERSION = $TRAVIS_NODE_VERSION
5+
6+
if [ $TRAVIS_NODE_VERSION -eq '6' ];then
7+
tnvm install alinode-v2.9.0
8+
tnvm use alinode-v2.9.0
9+
elif [ $TRAVIS_NODE_VERSION -eq '8' ];then
10+
tnvm install alinode-v3.16.0
11+
tnvm use alinode-v3.16.0
12+
elif [ $TRAVIS_NODE_VERSION -eq '10' ];then
13+
tnvm install alinode-v4.13.2
14+
tnvm use alinode-v4.13.2
15+
else
16+
tnvm install alinode-v6.4.4
17+
tnvm use alinode-v6.4.4
18+
fi
19+
20+
tnvm current
21+
22+
echo 'which node'
23+
which node
24+
25+
source $HOME/.bash_profile
26+
source $HOME/.bashrc
27+

lib/orders/error_log.js

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var fs = require('fs');
44
var helper = require('../utils');
55
var Parser = require('../error_parser');
6+
var glob = require('glob');
67

78
var MAX_ERROR_COUNT = 20; // 一次最多抓获20个Error
89
exports.logs = []; // 日志路径
@@ -11,10 +12,14 @@ var keyMap = new Map(); // 记录每个key实际对应的路径值
1112
var map = new Map(); // 记录每个文件访问的位置
1213
var parsers = new Map(); // 每个key都有一个parser
1314

14-
var getRealPath = function (filepath) {
15-
return helper.resolveYYYYMMDDHH(filepath);
15+
var getRealPath = function (filepath,callback) {
16+
glob(filepath,callback);
1617
};
1718

19+
// var a = function (err,file){
20+
// return file.map(path => helper.resolveYYYYMMDDHH(path))
21+
// }
22+
1823
var readFile = function (key, filepath, callback) {
1924
fs.stat(filepath, function (err, stats) {
2025
if (err) {
@@ -63,20 +68,36 @@ var readFile = function (key, filepath, callback) {
6368
};
6469

6570
var readLog = function (key, callback) {
66-
var currentPath = getRealPath(key);
67-
var current = keyMap.get(key);
68-
69-
if (currentPath !== current) {
70-
keyMap.set(key, currentPath); // replace real path
71-
readFile(key, current, function (err) {
72-
if (err) {
73-
return callback(err);
71+
getRealPath(key,function(_err,files){
72+
var currentPaths = files.map(path => helper.resolveYYYYMMDDHH(path));
73+
var currents = keyMap.get(key);
74+
75+
if (currentPaths.toString() !== currents.toString()) {
76+
keyMap.set(key, currentPaths); // replace real path
77+
currents.forEach(current => {
78+
readFile(key, current, function (err) {
79+
if (err) {
80+
return callback(err);
81+
}
82+
if( currentPaths.length !== 0) {
83+
currentPaths.forEach(path => {
84+
readFile(key, path, callback);
85+
});
86+
} else {
87+
readFile(key, '', callback);
88+
}
89+
});
90+
});
91+
} else {
92+
if( currentPaths.length !== 0) {
93+
currentPaths.forEach(path => {
94+
readFile(key, path, callback);
95+
});
96+
}else{
97+
readFile(key, '', callback);
7498
}
75-
readFile(key, currentPath, callback);
76-
});
77-
} else {
78-
readFile(key, currentPath, callback);
79-
}
99+
}
100+
});
80101
};
81102

82103
var readLogs = function (callback) {
@@ -99,14 +120,20 @@ exports.init = function (config) {
99120
exports.logs = config.error_log;
100121
var logs = config.error_log;
101122

123+
var key;
124+
var initCallBack = function(_err,files){
125+
var realPath = files.map(path => helper.resolveYYYYMMDDHH(path));
126+
realPath.forEach(path => {
127+
if (fs.existsSync(path)) {
128+
map.set(path, fs.statSync(path).size);
129+
}
130+
});
131+
keyMap.set(key, realPath);
132+
};
102133
for (var i = 0; i < logs.length; i++) {
103-
var key = logs[i];
134+
key = logs[i];
104135
parsers.set(key, new Parser(MAX_ERROR_COUNT));
105-
var realPath = getRealPath(key);
106-
if (fs.existsSync(realPath)) {
107-
map.set(realPath, fs.statSync(realPath).size);
108-
}
109-
keyMap.set(key, realPath);
136+
getRealPath(key,initCallBack);
110137
}
111138
}
112139
};

test/orders/error_log.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,21 @@ describe('/lib/orders/error_log.js', function () {
9595
});
9696
});
9797

98+
describe('wildcard match logs', function () {
99+
before(function () {
100+
errorLog.init({
101+
error_log: [path.join(__dirname, '../logs', '*.log')]
102+
});
103+
});
104+
105+
it('should ok', function (done) {
106+
errorLog.run(function (err) {
107+
expect(err).to.not.be.ok();
108+
done();
109+
});
110+
});
111+
});
112+
98113
describe('when path is folder error logs', function () {
99114
before(function () {
100115
errorLog.init({

0 commit comments

Comments
 (0)