|
@@ -106,13 +106,13 @@ function getLastFile(dirPath, number, callback) {
|
|
|
const stats = await fs.promises.stat(filePath);
|
|
|
if (stats.isFile()) {
|
|
|
fileList.push({name: file, time: stats.birthtime.getTime()});
|
|
|
- // logger.info("文件:" + filePath + "时间:" + stats.birthtime.getTime())
|
|
|
+ // logger.info("----------------文件:" + filePath + "时间:" + stats.birthtime.getTime())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
fileList.sort((a, b) => b.time - a.time);
|
|
|
// for (a of fileList) {
|
|
|
- // logger.info("文件cccccccc22222222:" + a.name + "\ttime:" + a.time)
|
|
|
+ // logger.info("cccccccccc文件cccccccc22222222:" + a.name + "\ttime:" + a.time)
|
|
|
// }
|
|
|
lastFileList = fileList.slice(0, number != -1 ? number : fileList.length);
|
|
|
lastFileNameList = lastFileList.map((item) => item.name);
|
|
@@ -125,11 +125,6 @@ function getLastFile(dirPath, number, callback) {
|
|
|
// 读取日志(倒叙)指定行数
|
|
|
async function readLastNLines(dirPath, filePathList, n) {
|
|
|
try {
|
|
|
- // for (var i = 0; i < filePathList.length; i++) {
|
|
|
- // var item = filePathList[i];
|
|
|
- // logger.info("日志文件2222222:" + item)
|
|
|
- // }
|
|
|
-
|
|
|
const fileList = [];
|
|
|
var count = 0;
|
|
|
for (var i = filePathList.length - 1; i >= 0; i--) {
|
|
@@ -155,9 +150,6 @@ async function readLastNLines(dirPath, filePathList, n) {
|
|
|
}
|
|
|
fileList.reverse();
|
|
|
|
|
|
- // const allFile = [].concat(...fileList.map((item) => item.lines));
|
|
|
- // let lastNLines = allFile.slice(-n).join("\n");
|
|
|
- // return lastNLines.split("\n").reverse();
|
|
|
|
|
|
const allFile = [].concat(...fileList.map((item) => item.lines));
|
|
|
const lastNLines = allFile.slice(-n).reverse();
|
|
@@ -167,6 +159,35 @@ async function readLastNLines(dirPath, filePathList, n) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+async function getRecentLogs(dirPath,logFiles, requiredLogs = 500) {
|
|
|
+ let logs = [];
|
|
|
+
|
|
|
+ for (const file of logFiles) {
|
|
|
+ let log = [];
|
|
|
+ // 创建文件流
|
|
|
+ const filepath = `${dirPath}/${file}`;
|
|
|
+ const fileStream = fs.createReadStream(filepath);
|
|
|
+
|
|
|
+ const rl = readline.createInterface({
|
|
|
+ input: fileStream,
|
|
|
+ crlfDelay: Infinity
|
|
|
+ });
|
|
|
+
|
|
|
+ for await (const line of rl) {
|
|
|
+ // logs.push(line);
|
|
|
+ log.push(line);
|
|
|
+ // 如果已读取到必要的日志条数,则停止
|
|
|
+ if (logs.length >= requiredLogs) {
|
|
|
+ return logs.slice(0, requiredLogs);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ logs.push(...log.reverse());
|
|
|
+ }
|
|
|
+
|
|
|
+ return logs; // 如果日志不够500条,返回现有日志
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
async function readLinesFromEnd(filePath, maxLines) {
|
|
|
const lines = [];
|
|
@@ -270,5 +291,6 @@ module.exports = {
|
|
|
writeFile,
|
|
|
copyFileSync,
|
|
|
readLastNLines,
|
|
|
+ getRecentLogs,
|
|
|
getLastFile
|
|
|
}
|