|
|
@ -1,10 +1,13 @@
|
|
|
|
'use strict'
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const fs = require('fs');
|
|
|
|
const split = require('split2');
|
|
|
|
const split = require('split2');
|
|
|
|
const assingDeep = require('assign-deep');
|
|
|
|
const assingDeep = require('assign-deep');
|
|
|
|
const { Transform } = require('stream');
|
|
|
|
const { Transform } = require('stream');
|
|
|
|
const DEV_ENVS = ['test', 'development'];
|
|
|
|
const DEV_ENVS = ['test', 'development'];
|
|
|
|
const logs = new Map();
|
|
|
|
const dumpPath = `${__dirname}/dump.json`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let logs;
|
|
|
|
|
|
|
|
|
|
|
|
const LEVELS = {
|
|
|
|
const LEVELS = {
|
|
|
|
10: 'trace',
|
|
|
|
10: 'trace',
|
|
|
@ -16,7 +19,7 @@ const LEVELS = {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = function logCollector () {
|
|
|
|
module.exports = function logCollector () {
|
|
|
|
return new Transform({
|
|
|
|
const stream = new Transform({
|
|
|
|
transform (chunk, enc, callback) {
|
|
|
|
transform (chunk, enc, callback) {
|
|
|
|
let entry;
|
|
|
|
let entry;
|
|
|
|
|
|
|
|
|
|
|
@ -34,7 +37,7 @@ module.exports = function logCollector () {
|
|
|
|
return callback();
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const { id, end } = entry;
|
|
|
|
const { id } = entry;
|
|
|
|
|
|
|
|
|
|
|
|
if (id === undefined) {
|
|
|
|
if (id === undefined) {
|
|
|
|
entry.level = LEVELS[entry.level];
|
|
|
|
entry.level = LEVELS[entry.level];
|
|
|
@ -42,8 +45,11 @@ module.exports = function logCollector () {
|
|
|
|
return callback();
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (end === true) {
|
|
|
|
if (logs.has(id)) {
|
|
|
|
const accEntry = logs.get(id);
|
|
|
|
const accEntry = logs.get(id);
|
|
|
|
|
|
|
|
const { end } = entry;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (end === true) {
|
|
|
|
accEntry.level = LEVELS[accEntry.level];
|
|
|
|
accEntry.level = LEVELS[accEntry.level];
|
|
|
|
accEntry.time = entry.time;
|
|
|
|
accEntry.time = entry.time;
|
|
|
|
this.push(`${JSON.stringify(accEntry)}\n`);
|
|
|
|
this.push(`${JSON.stringify(accEntry)}\n`);
|
|
|
@ -51,9 +57,6 @@ module.exports = function logCollector () {
|
|
|
|
return callback();
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (logs.has(id)) {
|
|
|
|
|
|
|
|
const accEntry = logs.get(id);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (accEntry.level > entry.level) {
|
|
|
|
if (accEntry.level > entry.level) {
|
|
|
|
delete entry.level
|
|
|
|
delete entry.level
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -70,7 +73,32 @@ module.exports = function logCollector () {
|
|
|
|
|
|
|
|
|
|
|
|
callback();
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
stream.on('pipe', () => {
|
|
|
|
|
|
|
|
if (!fs.existsSync(dumpPath)) {
|
|
|
|
|
|
|
|
logs = new Map();
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
const dump = require(dumpPath);
|
|
|
|
|
|
|
|
logs = new Map(dump);
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
console.error(`Cannot read the dump for unfinished logs: ${err}`);
|
|
|
|
|
|
|
|
logs = new Map();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
stream.on('unpipe', () => {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
fs.writeFileSync(dumpPath, JSON.stringify([...logs]));
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
console.error(`Cannot create a dump for unfinished logs: ${err}`);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function hasProperty(obj, prop) {
|
|
|
|
function hasProperty(obj, prop) {
|
|
|
|