Заставить работать функцию-транспорт логера Pino совместно с библиотекой Inquirer.js в CLI приложении на node.js
Inquirer.js обрабатывает пользовательский ввод в консоль. При использовании функции-транспорта логера Pino совместно с Inquirer.js не получается перенаправлять поток вывода данныйx stdout.
В приложении есть файл index.js:
'use strict';
const inquirer = require('inquirer');
const { getLogger } = require(`./logger`);
const logger = getLogger();
const test = function (answer) {
logger.info(JSON.parse(answer));
};
const questions = [
{
type: 'input',
name: 'question',
message: 'question to user',
default: function () {
return '';
},
},
];
inquirer.prompt(questions).then((answer) => {
test(JSON.stringify(answer, null, ' '));
});
файл logger.js:
'use strict';
let n = 0;
const logger = require(`pino`)({
name: `test-pino`,
level: process.env.LOG_LEVEL || `info`,
mixin() {
return { line: ++n };
},
});
module.exports = {
logger,
getLogger(options = {}) {
return logger.child(options);
},
};
и файл transport.js
'use strict';
const split = require('split2');
const pump = require('pump');
const through = require('through2');
const myTransport = through.obj(function (chunk, enc, cb) {
// do the necessary
console.log(chunk);
cb();
});
pump(process.stdin, split(JSON.parse), myTransport);
в консоле использую команду LOG_LEVEL=debug node index.js | node transport.js или node index.js | node transport.js.
В результате, функция транспорт в transport.js не работает. Но если не использовать Inquirer.js, то все прекрасно работает:
index.js:
'use strict';
const { getLogger } = require(`./logger`);
const logger = getLogger();
const test = function () {
logger.info('it works');
};
test();
вывод в консоль:
{
level: 30,
time: 1602263541760,
pid: 14248,
hostname: 'DESKTOP-9IG2DCD',
name: 'test-pino',
line: 1,
msg: 'it works'
}
Как заставить pino и Inquirer.js работать вместе?