-
Notifications
You must be signed in to change notification settings - Fork 301
Expand file tree
/
Copy pathlogReader.js
More file actions
27 lines (21 loc) · 821 Bytes
/
logReader.js
File metadata and controls
27 lines (21 loc) · 821 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import fs from '../fsWrapper.js';
import path from 'path';
export async function readLogLines(lines = 50) {
const logDir = path.join(process.cwd(), 'logs');
if (!await fs.pathExists(logDir)) {
return { file: null, content: '日志目录不存在' };
}
const files = await fs.readdir(logDir);
const logFiles = files
.filter(f => f.endsWith('.log.txt'))
.sort()
.reverse();
if (logFiles.length === 0) {
return { file: null, content: '没有日志文件' };
}
const latestLog = path.join(logDir, logFiles[0]);
const content = await fs.readFile(latestLog, 'utf-8');
const allLines = content.trim().split('\n');
const lastLines = allLines.slice(-lines);
return { file: logFiles[0], content: lastLines.join('\n'), lastLines };
}