From e206a1bca3f00a132156bd623d1fc49e12b1b1cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Mon, 8 Jun 2020 16:34:08 +0200 Subject: [PATCH] Check whether the log is a pino's log and skip them when they aren't --- log-collector.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/log-collector.js b/log-collector.js index 2b4c1f1c..c079a7fc 100644 --- a/log-collector.js +++ b/log-collector.js @@ -2,8 +2,9 @@ const split = require('split2'); const assingDeep = require('assign-deep'); -const logs = new Map(); const { Transform } = require('readable-stream'); +const DEV_ENVS = ['test', 'development']; +const logs = new Map(); const LEVELS = { 10: 'trace', @@ -21,8 +22,15 @@ function logTransport () { try { entry = JSON.parse(chunk); - } catch (error) { - // this.push(chunk + '\n'); + const { level, time } = entry; + + if (level === undefined && time === undefined) { + throw new Error('Entry log is not a valid'); + } + } catch (e) { + if (DEV_ENVS.includes(process.env.NODE_ENV)) { + this.push(chunk + '\n'); + } return callback(); } @@ -51,7 +59,7 @@ function logTransport () { } let error; - if (Object.prototype.hasOwnProperty.call(accEntry, 'error') && Object.prototype.hasOwnProperty.call(entry, 'error')) { + if (hasProperty(accEntry, 'error') && hasProperty(entry, 'error')) { logs.set(id, assingDeep({}, accEntry, entry, { error: accEntry.error.concat(entry.error) })); } else { logs.set(id, assingDeep({}, accEntry, entry)); @@ -65,6 +73,10 @@ function logTransport () { }) } +function hasProperty(obj, prop) { + return Object.prototype.hasOwnProperty.call(obj, prop) +} + process.stdin .pipe(split()) .pipe(logTransport())