@abtnode/util 1.16.14-beta-0c29907f → 1.16.14-beta-d802cd3c
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -9
- package/lib/log.js +0 -392
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.16.14-beta-
|
|
6
|
+
"version": "1.16.14-beta-d802cd3c",
|
|
7
7
|
"description": "ArcBlock's JavaScript utility",
|
|
8
8
|
"main": "lib/index.js",
|
|
9
9
|
"files": [
|
|
@@ -18,9 +18,9 @@
|
|
|
18
18
|
"author": "polunzh <polunzh@gmail.com> (http://github.com/polunzh)",
|
|
19
19
|
"license": "Apache-2.0",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@abtnode/constant": "1.16.14-beta-
|
|
22
|
-
"@abtnode/logger": "1.16.14-beta-
|
|
23
|
-
"@blocklet/constant": "1.16.14-beta-
|
|
21
|
+
"@abtnode/constant": "1.16.14-beta-d802cd3c",
|
|
22
|
+
"@abtnode/logger": "1.16.14-beta-d802cd3c",
|
|
23
|
+
"@blocklet/constant": "1.16.14-beta-d802cd3c",
|
|
24
24
|
"@ocap/client": "1.18.87",
|
|
25
25
|
"@ocap/mcrypto": "1.18.87",
|
|
26
26
|
"@ocap/util": "1.18.87",
|
|
@@ -53,12 +53,10 @@
|
|
|
53
53
|
"pm2": "^5.3.0",
|
|
54
54
|
"public-ip": "^4.0.4",
|
|
55
55
|
"pump": "^3.0.0",
|
|
56
|
-
"read-last-lines": "^1.8.0",
|
|
57
56
|
"semver-sort": "^1.0.0",
|
|
58
57
|
"shelljs": "^0.8.5",
|
|
59
58
|
"slugify": "^1.6.5",
|
|
60
59
|
"stream-to-promise": "^3.0.0",
|
|
61
|
-
"tail": "^2.2.4",
|
|
62
60
|
"through2-filter": "^3.0.0",
|
|
63
61
|
"through2-map": "^3.0.0",
|
|
64
62
|
"to-semver": "^3.0.0",
|
|
@@ -71,8 +69,7 @@
|
|
|
71
69
|
"detect-port": "^1.5.1",
|
|
72
70
|
"express": "^4.18.2",
|
|
73
71
|
"fs-extra": "^10.1.0",
|
|
74
|
-
"jest": "^27.5.1"
|
|
75
|
-
"unzipper": "^0.10.11"
|
|
72
|
+
"jest": "^27.5.1"
|
|
76
73
|
},
|
|
77
|
-
"gitHead": "
|
|
74
|
+
"gitHead": "f8e2dcefd8d6876e5dedee833ac976b4a0496b20"
|
|
78
75
|
}
|
package/lib/log.js
DELETED
|
@@ -1,392 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-underscore-dangle */
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const createArchive = require('archiver');
|
|
4
|
-
const fs = require('fs-extra');
|
|
5
|
-
const glob = require('fast-glob');
|
|
6
|
-
const isEqual = require('lodash/isEqual');
|
|
7
|
-
const { Tail } = require('tail');
|
|
8
|
-
const readLastLines = require('read-last-lines');
|
|
9
|
-
const { BLOCKLET_MODES } = require('@blocklet/constant');
|
|
10
|
-
const logger = require('@abtnode/logger')(require('../package.json').name);
|
|
11
|
-
|
|
12
|
-
const dayjs = require('./dayjs');
|
|
13
|
-
|
|
14
|
-
class StreamLog {
|
|
15
|
-
constructor() {
|
|
16
|
-
this._files = null; // Object { <level>: <filePath> }
|
|
17
|
-
this._tails = null; // Object { <level>: <Tail> }
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* @param {Object} files Object { <level>: <filePath> }
|
|
22
|
-
* @return {Boolean} if files change
|
|
23
|
-
*/
|
|
24
|
-
setFiles(files) {
|
|
25
|
-
if (!isEqual(this._files, files)) {
|
|
26
|
-
this.clearTails();
|
|
27
|
-
this._files = files;
|
|
28
|
-
return true;
|
|
29
|
-
}
|
|
30
|
-
return false;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
getRecent(lineNum, callback) {
|
|
34
|
-
if (!this._files) {
|
|
35
|
-
callback(new Error('files is empty'));
|
|
36
|
-
}
|
|
37
|
-
Object.entries(this._files).forEach(([level, file]) => {
|
|
38
|
-
if (!file || !fs.existsSync(file)) {
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
readLastLines
|
|
42
|
-
.read(file, lineNum || 0)
|
|
43
|
-
.then((data) => {
|
|
44
|
-
callback(null, level, data);
|
|
45
|
-
})
|
|
46
|
-
.catch((error) => {
|
|
47
|
-
callback(error, level);
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
ensureTails() {
|
|
53
|
-
if (this._tails) {
|
|
54
|
-
return this._tails;
|
|
55
|
-
}
|
|
56
|
-
if (!this._files) {
|
|
57
|
-
return {};
|
|
58
|
-
}
|
|
59
|
-
try {
|
|
60
|
-
this._tails = {};
|
|
61
|
-
Object.entries(this._files).forEach(([level, file]) => {
|
|
62
|
-
if (!file || !fs.existsSync(file)) {
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// 测试发现 Windows 下 fs.watch 没有效果,但是 fs.watch 的性能更好,所以只在 windows 下使用 fs.watchFile
|
|
67
|
-
this._tails[level] = new Tail(file, {
|
|
68
|
-
useWatchFile: process.platform === 'win32',
|
|
69
|
-
fsWatchOptions: { interval: 1500 },
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
return this._tails;
|
|
73
|
-
} catch (error) {
|
|
74
|
-
this._tails = null;
|
|
75
|
-
throw error;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
clearTails() {
|
|
80
|
-
if (this._tails) {
|
|
81
|
-
Object.values(this._tails).forEach((tail) => {
|
|
82
|
-
tail.unwatch();
|
|
83
|
-
});
|
|
84
|
-
this._tails = null;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const createFile = (files) => {
|
|
90
|
-
Object.values(files).forEach((file) => {
|
|
91
|
-
if (!fs.existsSync(file)) {
|
|
92
|
-
try {
|
|
93
|
-
const dir = path.dirname(file);
|
|
94
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
95
|
-
} catch (err) {
|
|
96
|
-
// Do nothing
|
|
97
|
-
}
|
|
98
|
-
fs.writeFileSync(file, '', 'utf8');
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
const getLogFiles = async ({ name, node }) => {
|
|
104
|
-
const dt = new Date();
|
|
105
|
-
const { yyyy, mm, dd } = {
|
|
106
|
-
yyyy: dt.getFullYear(),
|
|
107
|
-
mm: `${dt.getMonth() + 1}`.padStart(2, 0),
|
|
108
|
-
dd: `${dt.getDate()}`.padStart(2, 0),
|
|
109
|
-
};
|
|
110
|
-
const date = `${yyyy}-${mm}-${dd}`;
|
|
111
|
-
|
|
112
|
-
if (name === 'abtnode') {
|
|
113
|
-
const logDir = path.join(node.dataDirs.logs, '_abtnode');
|
|
114
|
-
|
|
115
|
-
const info = path.join(logDir, `daemon-${date}.log`);
|
|
116
|
-
const error = path.join(logDir, `daemon-error-${date}.log`);
|
|
117
|
-
const access = path.join(logDir, 'access.log');
|
|
118
|
-
const stdout = path.join(logDir, 'daemon.stdout.log');
|
|
119
|
-
const stderr = path.join(logDir, 'daemon.stderr.log');
|
|
120
|
-
|
|
121
|
-
createFile({
|
|
122
|
-
info,
|
|
123
|
-
error,
|
|
124
|
-
access,
|
|
125
|
-
stdout,
|
|
126
|
-
stderr,
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
return {
|
|
130
|
-
info,
|
|
131
|
-
error,
|
|
132
|
-
access,
|
|
133
|
-
stdout,
|
|
134
|
-
stderr,
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
if (name === 'blocklet-services') {
|
|
139
|
-
const logDir = path.join(node.dataDirs.logs, '_abtnode');
|
|
140
|
-
const info = path.join(logDir, 'service.log');
|
|
141
|
-
const error = path.join(logDir, 'service.error.log');
|
|
142
|
-
createFile({ info, error });
|
|
143
|
-
return { info, error };
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
if (name.indexOf('blocklet-') === 0) {
|
|
147
|
-
const did = name.substring('blocklet-'.length);
|
|
148
|
-
const blocklet = await node.getBlocklet({ did, attachConfig: false });
|
|
149
|
-
const { name: blockletName } = blocklet.meta;
|
|
150
|
-
|
|
151
|
-
const dir = path.join(node.dataDirs.logs, blockletName);
|
|
152
|
-
const info = path.join(dir, `info-${date}.log`);
|
|
153
|
-
const error = path.join(dir, `info-error-${date}.log`);
|
|
154
|
-
const access = path.join(dir, 'access.log');
|
|
155
|
-
const stdout = path.join(dir, 'output.log');
|
|
156
|
-
const stderr = path.join(dir, 'error.log');
|
|
157
|
-
|
|
158
|
-
createFile({
|
|
159
|
-
info,
|
|
160
|
-
error,
|
|
161
|
-
access,
|
|
162
|
-
stdout,
|
|
163
|
-
stderr,
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
return {
|
|
167
|
-
info,
|
|
168
|
-
error,
|
|
169
|
-
access,
|
|
170
|
-
stdout,
|
|
171
|
-
stderr,
|
|
172
|
-
recent: blocklet.mode === BLOCKLET_MODES.DEVELOPMENT ? 0 : 100,
|
|
173
|
-
};
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
if (name.startsWith('service-gateway-')) {
|
|
177
|
-
const providerName = name.substring('service-gateway-'.length);
|
|
178
|
-
const provider = node.getRouterProvider(providerName);
|
|
179
|
-
if (!provider) {
|
|
180
|
-
logger.error('router engine is empty', { name, providerName });
|
|
181
|
-
return {};
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
return provider.getLogFilesForToday();
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
return {};
|
|
188
|
-
};
|
|
189
|
-
|
|
190
|
-
const getDownloadLogFilesFromServer = async ({ dates, nodeInfo, node } = {}) => {
|
|
191
|
-
const logDir = path.join(node.dataDirs.logs, '_abtnode');
|
|
192
|
-
|
|
193
|
-
const pm2Log = path.join(logDir, 'pm2.log');
|
|
194
|
-
const pm2LogSrc = path.join(process.env.PM2_HOME, 'pm2.log');
|
|
195
|
-
if (fs.existsSync(pm2LogSrc)) {
|
|
196
|
-
await fs.copy(pm2LogSrc, pm2Log);
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
const provider = node.getRouterProvider(nodeInfo.routing.provider);
|
|
200
|
-
if (!provider) {
|
|
201
|
-
logger.error('router engine is empty');
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
const list = [];
|
|
205
|
-
|
|
206
|
-
dates.forEach((d) => {
|
|
207
|
-
// log file created by @abtnode/log
|
|
208
|
-
// log file created by abt-node-log-rotate
|
|
209
|
-
list.push(path.join(logDir, `*-${d}*`));
|
|
210
|
-
|
|
211
|
-
// log file create by router
|
|
212
|
-
const routerLogDir = provider.getLogDir();
|
|
213
|
-
if (routerLogDir) {
|
|
214
|
-
list.push(path.join(routerLogDir, `*-${d}*`));
|
|
215
|
-
}
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
// abt-node-daemon console & gateway
|
|
219
|
-
list.push(path.join(logDir, 'daemon.stdout.log*'));
|
|
220
|
-
list.push(path.join(logDir, 'daemon.stderr.log*'));
|
|
221
|
-
list.push(path.join(logDir, 'access.log*'));
|
|
222
|
-
|
|
223
|
-
// abt-node-service console & gateway
|
|
224
|
-
list.push(path.join(logDir, 'service.output.log*'));
|
|
225
|
-
list.push(path.join(logDir, 'service.error.log*'));
|
|
226
|
-
list.push(path.join(logDir, 'service.log*'));
|
|
227
|
-
|
|
228
|
-
// abt-node-db-hub console & backup
|
|
229
|
-
list.push(path.join(logDir, 'db.output.log*'));
|
|
230
|
-
list.push(path.join(logDir, 'db.error.log*'));
|
|
231
|
-
|
|
232
|
-
// abt-node-event-hub console
|
|
233
|
-
list.push(path.join(logDir, 'event.output.log*'));
|
|
234
|
-
list.push(path.join(logDir, 'event.error.log*'));
|
|
235
|
-
|
|
236
|
-
// abt-node-log-rotate console
|
|
237
|
-
list.push(path.join(logDir, 'pm2-logrotate.stdout.log*'));
|
|
238
|
-
list.push(path.join(logDir, 'pm2-logrotate.stderr.log*'));
|
|
239
|
-
|
|
240
|
-
// abt-node-updater console
|
|
241
|
-
list.push(path.join(logDir, 'updater.error.log*'));
|
|
242
|
-
list.push(path.join(logDir, 'updater.output.log*'));
|
|
243
|
-
|
|
244
|
-
// fallback log
|
|
245
|
-
list.push(path.join(logDir, 'stderr.log*'));
|
|
246
|
-
list.push(path.join(logDir, 'stdout.log*'));
|
|
247
|
-
|
|
248
|
-
// router
|
|
249
|
-
list.push(...Object.values(provider.getLogFilesForToday() || {}));
|
|
250
|
-
|
|
251
|
-
// pm2 log
|
|
252
|
-
list.push(pm2Log);
|
|
253
|
-
|
|
254
|
-
return glob(list);
|
|
255
|
-
};
|
|
256
|
-
|
|
257
|
-
const getDownloadLogFilesFromBlocklet = ({ dates, blocklet } = {}) => {
|
|
258
|
-
const logDir = path.join(blocklet.env.logsDir);
|
|
259
|
-
|
|
260
|
-
const list = [];
|
|
261
|
-
|
|
262
|
-
dates.forEach((d) => {
|
|
263
|
-
// log file created by @abtnode/log
|
|
264
|
-
// log file created by abt-node-log-rotate
|
|
265
|
-
list.push(path.join(logDir, `*-${d}*`));
|
|
266
|
-
});
|
|
267
|
-
|
|
268
|
-
list.push(path.join(logDir, 'output.log*'));
|
|
269
|
-
list.push(path.join(logDir, 'error.log*'));
|
|
270
|
-
list.push(path.join(logDir, 'access.log*'));
|
|
271
|
-
|
|
272
|
-
return glob(list);
|
|
273
|
-
};
|
|
274
|
-
|
|
275
|
-
const getDownloadLogFiles = async ({ did, node, days = 1, now } = {}) => {
|
|
276
|
-
const dates = [dayjs(now).format('YYYY-MM-DD')];
|
|
277
|
-
|
|
278
|
-
for (let i = 1; i <= days; i++) {
|
|
279
|
-
dates.unshift(dayjs(now).subtract(i, 'day').format('YYYY-MM-DD'));
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
const nodeInfo = await node.getNodeInfo();
|
|
283
|
-
|
|
284
|
-
if (nodeInfo.did === did) {
|
|
285
|
-
return getDownloadLogFilesFromServer({ dates, node, nodeInfo });
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
const blocklet = await node.getBlocklet({ did });
|
|
289
|
-
if (!blocklet) {
|
|
290
|
-
throw new Error('blocklet not found');
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
return getDownloadLogFilesFromBlocklet({ dates, blocklet });
|
|
294
|
-
};
|
|
295
|
-
|
|
296
|
-
const createStreamLogManager = ({ onLog, onGetLogFiles }) => {
|
|
297
|
-
const store = {}; // Object<name: streamLog>
|
|
298
|
-
|
|
299
|
-
const ensure = (name) => {
|
|
300
|
-
if (!store[name]) {
|
|
301
|
-
store[name] = new StreamLog();
|
|
302
|
-
}
|
|
303
|
-
return store[name];
|
|
304
|
-
};
|
|
305
|
-
|
|
306
|
-
const destroy = (name) => {
|
|
307
|
-
logger.info('log stream: destroy', { name });
|
|
308
|
-
if (!store[name]) {
|
|
309
|
-
return;
|
|
310
|
-
}
|
|
311
|
-
const log = store[name];
|
|
312
|
-
try {
|
|
313
|
-
log.clearTails();
|
|
314
|
-
delete store[name];
|
|
315
|
-
} catch (error) {
|
|
316
|
-
logger.error('log stream: remove ref error ', error);
|
|
317
|
-
delete store[name];
|
|
318
|
-
}
|
|
319
|
-
};
|
|
320
|
-
|
|
321
|
-
const add = async (name, topic, firstLogCb) => {
|
|
322
|
-
logger.info('log stream: add', { name });
|
|
323
|
-
const log = ensure(name);
|
|
324
|
-
try {
|
|
325
|
-
// update files
|
|
326
|
-
// push recent 100 log
|
|
327
|
-
const { recent = 100, ...logFiles } = await onGetLogFiles(name);
|
|
328
|
-
const changed = await log.setFiles(logFiles);
|
|
329
|
-
logger.info('log stream: added', { name, logFiles });
|
|
330
|
-
log.getRecent(recent, (error, level, data) => {
|
|
331
|
-
if (error) {
|
|
332
|
-
logger.error('log stream error ', error);
|
|
333
|
-
}
|
|
334
|
-
if (firstLogCb) {
|
|
335
|
-
firstLogCb(level, data, logFiles);
|
|
336
|
-
}
|
|
337
|
-
});
|
|
338
|
-
// stream
|
|
339
|
-
if (changed) {
|
|
340
|
-
const tails = log.ensureTails();
|
|
341
|
-
Object.entries(tails).forEach(([level, tail]) => {
|
|
342
|
-
tail.on('line', (data) => {
|
|
343
|
-
onLog({ topic, level, logFiles, data });
|
|
344
|
-
});
|
|
345
|
-
tail.on('error', (error) => {
|
|
346
|
-
logger.error('log tail error ', { error });
|
|
347
|
-
destroy(name);
|
|
348
|
-
});
|
|
349
|
-
});
|
|
350
|
-
}
|
|
351
|
-
} catch (error) {
|
|
352
|
-
logger.error('log stream error ', error);
|
|
353
|
-
}
|
|
354
|
-
};
|
|
355
|
-
|
|
356
|
-
return {
|
|
357
|
-
add,
|
|
358
|
-
destroy,
|
|
359
|
-
};
|
|
360
|
-
};
|
|
361
|
-
|
|
362
|
-
const createDownloadLogStream = async ({ node, did, days, now }) => {
|
|
363
|
-
const files = await getDownloadLogFiles({ node, did, days, now });
|
|
364
|
-
|
|
365
|
-
if (!files.length) {
|
|
366
|
-
throw new Error('Log file does not found');
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
const cwd = path.dirname(node.dataDirs.logs);
|
|
370
|
-
|
|
371
|
-
const archive = createArchive('zip', { zlib: { level: 9 } });
|
|
372
|
-
files.forEach((x) => {
|
|
373
|
-
archive.file(x, {
|
|
374
|
-
name: x.replace(`${cwd}/logs`, '/logs').replace(`${cwd}`, '/logs').replace('/_abtnode', '/blocklet-server'),
|
|
375
|
-
});
|
|
376
|
-
});
|
|
377
|
-
|
|
378
|
-
archive.rawPipe = archive.pipe.bind(archive);
|
|
379
|
-
archive.pipe = (s) => {
|
|
380
|
-
archive.rawPipe(s);
|
|
381
|
-
archive.finalize();
|
|
382
|
-
};
|
|
383
|
-
|
|
384
|
-
return archive;
|
|
385
|
-
};
|
|
386
|
-
|
|
387
|
-
module.exports = {
|
|
388
|
-
createStreamLogManager,
|
|
389
|
-
getLogFiles,
|
|
390
|
-
getDownloadLogFiles,
|
|
391
|
-
createDownloadLogStream,
|
|
392
|
-
};
|