-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathruntime-log.js
More file actions
59 lines (52 loc) · 1.39 KB
/
Copy pathruntime-log.js
File metadata and controls
59 lines (52 loc) · 1.39 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* 进程内环形运行日志(供管理后台「运行日志」页展示)。
* 重启后清空;与 task_logs 表独立。
*/
const MAX_ENTRIES = 15000;
const MAX_LINE = 8192;
let seq = 1;
const buffer = [];
function push(entry) {
const text = String(entry.text || '').replace(/\r/g, '').slice(0, MAX_LINE);
if (!text.trim()) {
return;
}
const id = seq++;
buffer.push({
id,
ts: Date.now(),
jobKey: String(entry.jobKey || '').slice(0, 80),
level: String(entry.level || 'log').slice(0, 24),
source: String(entry.source || '').slice(0, 64),
text
});
while (buffer.length > MAX_ENTRIES) {
buffer.shift();
}
}
function tail(limit) {
const n = Math.min(MAX_ENTRIES, Math.max(1, Number(limit) || 500));
return buffer.slice(-n);
}
function after(afterId, limit) {
const aid = Math.max(0, Number(afterId) || 0);
const n = Math.min(2000, Math.max(1, Number(limit) || 500));
const out = [];
for (let i = 0; i < buffer.length; i += 1) {
const e = buffer[i];
if (e.id > aid) {
out.push(e);
if (out.length >= n) {
break;
}
}
}
return out;
}
function clear() {
buffer.length = 0;
}
function stats() {
return { count: buffer.length, nextSeq: seq };
}
module.exports = { push, tail, after, clear, stats };