Skip to content

Commit 1502e46

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

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

lib/orders/error_log.js

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
var fs = require('fs');
44
var helper = require('../utils');
55
var Parser = require('../error_parser');
6+
const { promisify } = require('util');
7+
var glob = promisify(require('glob'));
68

79
var MAX_ERROR_COUNT = 20; // 一次最多抓获20个Error
810
exports.logs = []; // 日志路径
@@ -11,8 +13,10 @@ var keyMap = new Map(); // 记录每个key实际对应的路径值
1113
var map = new Map(); // 记录每个文件访问的位置
1214
var parsers = new Map(); // 每个key都有一个parser
1315

14-
var getRealPath = function (filepath) {
15-
return helper.resolveYYYYMMDDHH(filepath);
16+
var getRealPath = async function (filepath) {
17+
var files = await glob(filepath)
18+
return files.map(path => helper.resolveYYYYMMDDHH(path))
19+
1620
};
1721

1822
var readFile = function (key, filepath, callback) {
@@ -62,20 +66,26 @@ var readFile = function (key, filepath, callback) {
6266
});
6367
};
6468

65-
var readLog = function (key, callback) {
66-
var currentPath = getRealPath(key);
67-
var current = keyMap.get(key);
69+
var readLog = async function (key, callback) {
70+
var currentPaths = await getRealPath(key);
71+
var currents = keyMap.get(key);
6872

69-
if (currentPath !== current) {
73+
if (currentPaths.toString() !== currents.toString()) {
7074
keyMap.set(key, currentPath); // replace real path
71-
readFile(key, current, function (err) {
72-
if (err) {
73-
return callback(err);
74-
}
75-
readFile(key, currentPath, callback);
76-
});
75+
currents.forEach(current => {
76+
readFile(key, current, function (err) {
77+
if (err) {
78+
return callback(err);
79+
}
80+
currentPaths.forEach(path => {
81+
readFile(key, path, callback);
82+
})
83+
});
84+
})
7785
} else {
78-
readFile(key, currentPath, callback);
86+
currentPaths.forEach(path => {
87+
readFile(key, path, callback);
88+
})
7989
}
8090
};
8191

@@ -94,18 +104,20 @@ var readLogs = function (callback) {
94104
}
95105
};
96106

97-
exports.init = function (config) {
107+
exports.init = async function (config) {
98108
if (config.error_log) {
99109
exports.logs = config.error_log;
100110
var logs = config.error_log;
101111

102112
for (var i = 0; i < logs.length; i++) {
103113
var key = logs[i];
104114
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-
}
115+
var realPath = await getRealPath(key);
116+
realPath.forEach(path => {
117+
if (fs.existsSync(path)) {
118+
map.set(path, fs.statSync(path).size);
119+
}
120+
})
109121
keyMap.set(key, realPath);
110122
}
111123
}

0 commit comments

Comments
 (0)